943,603 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 781
  • C++ RSS
Jul 8th, 2008
0

function not working cant figure problem

Expand Post »
been working on this code for 2-3 hours using the basic c++ information. tearing out my hair trying to figure out what is wrong writing hangman programm but trying to use functions to initialize programm and enter and response computer saying little or no error but wont launch can't figure out what to do plz help me. there are two types ive tried

OPTION 1:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <cctype>
  7.  
  8. using namespace std;
  9.  
  10. char getGuess();
  11.  
  12. string inWord();
  13.  
  14. int main()
  15. {
  16. // set-up
  17. const int MAX_WRONG = 8;
  18.  
  19. vector<string> words;
  20. words.push_back("GUESS");
  21. words.push_back("HANGMAN");
  22. words.push_back("DIFFICULT");
  23.  
  24. srand(time(0));
  25. random_shuffle(words.begin(), words.end());
  26. const string THE_WORD = words[0];
  27. int wrong = 0;
  28. string soFar(THE_WORD.size(), '-');
  29. string used = "";
  30.  
  31. cout << "welcome to hangman!!";
  32.  
  33.  
  34. }char getGuess()
  35. {
  36. char guess;
  37. cout << "\n\nEnter your guess: ";
  38. cin >> guess;
  39. guess = toupper(guess);
  40. return guess;
  41. }
  42.  
  43. string inWord()
  44. {
  45. vector<string> words;
  46. char guess;
  47. const string THE_WORD = words[0];
  48. char guess1 = THE_WORD.find(guess);
  49. int wrong = 0;
  50. string soFar(THE_WORD.size(), '-');
  51. if (guess1 != string::npos)
  52. {
  53. cout << "That's right! " << guess << " is in the word.\n";
  54.  
  55.  
  56. for (int i = 0; i < THE_WORD.length(); ++i)
  57. if (THE_WORD[i] == guess)
  58. soFar[i] = guess;
  59. }
  60. else
  61. {
  62. cout << "Sorry, " << guess << " isn't in the word.\n";
  63. ++wrong;
  64. }
  65. return soFar;
  66. }
  67.  
  68. while ((wrong < MAX_WRONG) && (soFar != THE_WORD));
  69. {
  70. cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
  71. cout << "\nYou've used the following letters:\n" << used << endl;
  72. cout << "\nSo far, the word is:\n" << soFar << endl;
  73.  
  74. getGuess()
  75. while (used.find(guess) != string::npos)
  76. {
  77. cout << "\nYou've already guessed " << guess << endl;
  78. cout << "Enter your guess: ";
  79. cin >> guess;
  80. guess = toupper(guess);
  81. }
  82.  
  83. used += guess;
  84.  
  85. inWord()
  86.  
  87. // shut down
  88. if (wrong == MAX_WRONG)
  89. cout << "\nYou've been hanged!";
  90. else
  91. cout << "\nYou guessed it!";
  92.  
  93. cout << "\nThe word was " << THE_WORD << endl;
  94.  
  95. return 0;
  96. }
OPTION 2:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <cctype>
  7.  
  8. using namespace std;
  9.  
  10. char getGuess();
  11.  
  12. string inWord();
  13.  
  14. int main()
  15. {
  16. const int MAX_WRONG = 8;
  17.  
  18. char guess;
  19. vector<string> words;
  20. words.push_back("GUESS");
  21. words.push_back("HANGMAN");
  22. words.push_back("DIFFICULT");
  23.  
  24. srand(time(0));
  25. random_shuffle(words.begin(), words.end());
  26. const string THE_WORD = words[0];
  27. int wrong = 0;
  28. string soFar(THE_WORD.size(), '-');
  29. string used = "";
  30.  
  31. cout << "welcome to hangman!!";
  32.  
  33. while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
  34. {
  35. cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n";
  36. cout << "\nYou've used the following letters:\n" << used << endl;
  37. cout << "\nSo far, the word is:\n" << soFar << endl;
  38.  
  39. getGuess();
  40.  
  41. while (used.find(guess) != string::npos)
  42. {
  43. cout << "\nYou've already guessed " << guess << endl;
  44. cout << "Enter your guess: ";
  45. cin >> guess;
  46. guess = toupper(guess);
  47. }
  48.  
  49. used += guess;
  50.  
  51. inWord();
  52.  
  53.  
  54. if (wrong == MAX_WRONG)
  55.  
  56. cout << "\nYou've been hanged!";
  57. else
  58.  
  59. cout << "\nYou guessed it!";
  60.  
  61. cout << "\nThe word was " << THE_WORD << endl;
  62.  
  63. return 0;
  64. }
  65. }
  66.  
  67. char getGuess()
  68. {
  69. char guess;
  70. cout << "\n\nEnter your guess: ";
  71. cin >> guess;
  72. guess = toupper(guess);
  73. return guess;
  74. }
  75.  
  76. string inWord()
  77. {
  78. vector<string> words;
  79. char guess;
  80. const string THE_WORD = words[0];
  81. char guess1 = THE_WORD.find(guess);
  82. int wrong = 0;
  83. string soFar(THE_WORD.size(), '-');
  84. if (guess1 != string::npos)
  85. {
  86. cout << "That's right! " << guess << " is in the word.\n";
  87.  
  88.  
  89. for (int i = 0; i < THE_WORD.length(); ++i)
  90. if (THE_WORD[i] == guess)
  91. soFar[i] = guess;
  92. }
  93. else
  94. {
  95. cout << "Sorry, " << guess << " isn't in the word.\n";
  96. ++wrong;
  97. }
  98. return soFar;
  99. }
Last edited by Ancient Dragon; Jul 8th, 2008 at 8:42 am. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chickenlord500 is offline Offline
9 posts
since Jun 2008
Jul 8th, 2008
0

Re: function not working cant figure problem

In OPTION 2 comment the following line
//inWord();
in your main function.
Last edited by RenjithVR; Jul 8th, 2008 at 2:41 am.
Reputation Points: 12
Solved Threads: 7
Light Poster
RenjithVR is offline Offline
41 posts
since Mar 2008
Jul 8th, 2008
0

Re: function not working cant figure problem

And learn how to use CODE-TAGS as you were told before.
Last edited by Nick Evan; Jul 8th, 2008 at 3:54 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jul 8th, 2008
0

Re: function not working cant figure problem

Use code tags and we might help.

And you should really work on your code formatting. That looks terrible.
Reputation Points: 59
Solved Threads: 16
Junior Poster
hacker9801 is offline Offline
129 posts
since Aug 2007
Jul 8th, 2008
0

Re: function not working cant figure problem

Click to Expand / Collapse  Quote originally posted by hacker9801 ...

And you should really work on your code formatting. That looks terrible.
If the poster doesn't use code-tags, all formatting will be automatically lost, so the code might actually be properly formatted....
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jul 10th, 2008
0

Re: function not working cant figure problem

We dont use code-tags in our c++ tags but if you guys say they are suppose to be there i might have to self teach myself them because we are using he C++ beginning from thomson course technology. and it doesnt mention code-tags


Click to Expand / Collapse  Quote originally posted by niek_e ...
If the poster doesn't use code-tags, all formatting will be automatically lost, so the code might actually be properly formatted....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chickenlord500 is offline Offline
9 posts
since Jun 2008
Jul 10th, 2008
1

Re: function not working cant figure problem

>and it doesnt mention code-tags
Code tags are a feature of Daniweb's forum. You take your code and wrap it in code tags like so when you want to post it here:

[code=cplusplus]
<code from your book here>
[/code]

This preserves the formatting of your code rather than trimming all leading whitespace from it. With the cplusplus attribute, line numbers and color coding are also added. Here's how code looks without code tags:

#include <iostream>

int main()
{
for ( int i = 0; i < 10; i++ )
std::cout<<"Hello, world!\n";
}

And here's the same code with code tags:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. for ( int i = 0; i < 10; i++ )
  6. std::cout<<"Hello, world!\n";
  7. }
Notice the difference?

p.s. We give you about 10 posts to learn how to use code tags, then start issuing infractions for each failure to use them. At 2 points per infraction, that means you can get off five posts before you're automatically banned for not following our rules.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 10th, 2008
0

Re: function not working cant figure problem

In the function inword() , i dont understand why you are creating new vector words and a character variable guess.

Instead of doing that. Pass them as arguments for the function or declare them globally and dont declare them again in the function itself.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Transferring images..
Next Thread in C++ Forum Timeline: Re: Making a .dat file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC