function not working cant figure problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 7
Reputation: chickenlord500 is an unknown quantity at this point 
Solved Threads: 0
chickenlord500 chickenlord500 is offline Offline
Newbie Poster

function not working cant figure problem

 
0
  #1
Jul 8th, 2008
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:
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 41
Reputation: RenjithVR is an unknown quantity at this point 
Solved Threads: 7
RenjithVR RenjithVR is offline Offline
Light Poster

Re: function not working cant figure problem

 
0
  #2
Jul 8th, 2008
In OPTION 2 comment the following line
//inWord();
in your main function.
Last edited by RenjithVR; Jul 8th, 2008 at 2:41 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,867
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillacaphobiac

Re: function not working cant figure problem

 
0
  #3
Jul 8th, 2008
And learn how to use CODE-TAGS as you were told before.
Last edited by niek_e; Jul 8th, 2008 at 3:54 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 129
Reputation: hacker9801 is on a distinguished road 
Solved Threads: 15
hacker9801 hacker9801 is offline Offline
Junior Poster

Re: function not working cant figure problem

 
0
  #4
Jul 8th, 2008
Use code tags and we might help.

And you should really work on your code formatting. That looks terrible.
Don't yell at me if I'm wrong - I'm thirteen. :)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,867
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillacaphobiac

Re: function not working cant figure problem

 
0
  #5
Jul 8th, 2008
Originally Posted by hacker9801 View Post

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....
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 7
Reputation: chickenlord500 is an unknown quantity at this point 
Solved Threads: 0
chickenlord500 chickenlord500 is offline Offline
Newbie Poster

Re: function not working cant figure problem

 
0
  #6
Jul 10th, 2008
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


Originally Posted by niek_e View Post
If the poster doesn't use code-tags, all formatting will be automatically lost, so the code might actually be properly formatted....
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,698
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 728
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: function not working cant figure problem

 
1
  #7
Jul 10th, 2008
>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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: function not working cant figure problem

 
0
  #8
Jul 10th, 2008
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.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC