C++ Programming issue

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: C++ Programming issue

 
0
  #11
Apr 25th, 2007
I've reworked the full program a little bit. Note the new header file which eliminates the need for the sameWord function. Really look at and understand the use of multiple conditions in the while loop and the use of flags. The code as presented is untested and comes with no warrantees.
  1. #include <iostream>
  2. #include <cstring> //for functions to manipulate C style strings
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. char getWord[50];
  10. char guessString[50];
  11. char goodGuesses[99] = {'\0'};
  12. char badGuesses[99] = {'\0'};
  13. char guess;
  14. bool badGuess;
  15. int guessCount = 0;
  16. int guessCountLimit;
  17.  
  18. cout << "Please enter a word" << endl;
  19. cin >> getWord;
  20.  
  21. //place one '-' in guessString for every letter in getWord
  22. int i = 0;
  23. while(getWord[i])
  24. {
  25. guessString[i] = '-';
  26. i++;
  27. }
  28.  
  29. //end guessWord with null terminator
  30. guessString[i] = '\0';
  31.  
  32.  
  33. //calculate guessCountLimit
  34. guessCountLimit = i * 2;
  35.  
  36. cout << "Guess a letter: ";
  37. cin >> guess;
  38.  
  39. i = 0; //keep track of where you are in getWord and guessString
  40. int j = 0; //keep track of goodGuesses
  41. int k = 0; //keep track of badGuesses
  42.  
  43. bool incomplete = true; //means guessString != getWord
  44.  
  45. while(guessCount <= guessCountLimit && incomplete)
  46. {
  47. while(getWord[i] != '\0')
  48. {
  49. if(getWord[i] == guess)
  50. {
  51. guessString[i] = guess; //place correct guess in guessString
  52. badGuess = false; //set flag
  53. }
  54.  
  55. i++; //advance position in getWord
  56. }
  57.  
  58. if(badGuess)
  59. {
  60. badGuesses[k] = guess;
  61. k++; //advance position in badGuesses
  62. }
  63. else
  64. {
  65. goodGuesses[j] = guess;
  66. j++; //advance position in goodGuesses
  67. }
  68.  
  69. //display results so far
  70. cout << "Good guesses: " << goodGuesses << endl;
  71. cout << "Bad guesses: " << badGuesses << endl;
  72.  
  73. guessCount++;
  74.  
  75. //if guessString is now the same as getWord
  76. if(strcmp(guessString, getWord) == 0)
  77. {
  78. incomplete = false; //change the flag variable to stop the loop
  79. }
  80. else
  81. {
  82. //put the pressure on
  83. cout << "you have " << guessCountLimit - guessCount << " guesses left" << endl;
  84.  
  85. //get next guess
  86. cout << "Guess next letter: ";
  87. cin >> guess;
  88.  
  89. //reset variables
  90. i = 0;
  91. badGuess = true;
  92. }
  93. }
  94.  
  95. //now why did the loop stop
  96. if(incomplete)
  97. {
  98. cout << "Sorry, you lost" << endl;
  99. }
  100. else
  101. {
  102. cout << "You got it!" << endl;
  103. cout << "The word was: " << guessString << endl;
  104. }
  105. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: C++ Programming issue

 
0
  #12
Apr 27th, 2007
why not better work it out an even more simple way... get your string's length and create an array of characters as long as the string, which will substitute your string in the rest of the program. This way you can compare your guess individually with each element in the array...

i don't know... maybe it can make things a bit more simple...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: C++ Programming issue

 
0
  #13
Apr 27th, 2007
Originally Posted by Nichito View Post
why not better work it out an even more simple way... get your string's length and create an array of characters as long as the string, which will substitute your string in the rest of the program. This way you can compare your guess individually with each element in the array...

i don't know... maybe it can make things a bit more simple...
That's about what WaltP suggested
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: C++ Programming issue

 
0
  #14
Apr 27th, 2007
well, it's pretty near to what i said, though that gave me an idea... why not make a fusion of both ideas and create two individual arrays with the size of the string: one with '_' in each element, and another one with the letters in the string in it. When the user's guess is correct, you enter a for loop and assign the user's guess letter into the matching places in array1 (the one with '_') and array2 (the one with the letters)...

get it?
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 8
Reputation: Shad0wHawk is an unknown quantity at this point 
Solved Threads: 0
Shad0wHawk Shad0wHawk is offline Offline
Newbie Poster

Re: C++ Programming issue

 
0
  #15
Apr 28th, 2007
Thanks to all of you for your help. I got it!!!
Here is the final code...
  1. #include <iostream>
  2. #include <cstring> //for functions to manipulate C style strings
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. char getWord[50];
  10. char guessString[50];
  11. char goodGuesses[99] = {'\0'};
  12. char badGuesses[99] = {'\0'};
  13. char guess;
  14. bool badGuess = true;
  15. int guessCount = 0;
  16. int guessCountLimit;
  17.  
  18. cout << "Please enter a word: ";
  19. cin >> getWord;
  20.  
  21. system("cls");
  22.  
  23. //place one '-' in guessString for every letter in getWord
  24. int i = 0;
  25. while(guessString[i] != '\0')
  26. {
  27. while(getWord[i])
  28. {
  29. guessString[i] = '-';
  30. i++;
  31. }
  32. guessString[i] = '\0';
  33. cout << "The word has " << i << " letters" << "\t";
  34. cout << guessString << endl;
  35. }
  36. //end guessWord with null terminator
  37. guessString[i]='\0';
  38.  
  39.  
  40. //calculate guessCountLimit
  41. guessCountLimit = i * 2;
  42. cout << "You have " << guessCountLimit <<" guesses to start." << endl;
  43. cout << "Guess a letter: ";
  44. cin >> guess;
  45.  
  46. i = 0; //keep track of location in getWord and guessString
  47. int trackGood = 0; //keep track of goodGuesses
  48. int trackBad = 0; //keep track of badGuesses
  49.  
  50. bool incomplete = true; //means guessString != getWord
  51.  
  52. while(guessCount < guessCountLimit && incomplete)
  53. {
  54. while(getWord[i] != '\0')
  55. {
  56. if(getWord[i] == guess)
  57. {
  58. guessString[i] = guess; //place correct guess in guessString
  59. badGuess = false; //set flag
  60. }
  61.  
  62. cout << guessString[i];
  63. i++; //advance position in getWord
  64.  
  65. }
  66.  
  67. if(badGuess)
  68. {
  69. badGuesses[trackBad] = guess;
  70. trackBad++; //advance position in badGuesses
  71. }
  72. else
  73. {
  74. goodGuesses[trackGood] = guess;
  75. trackGood++; //advance position in goodGuesses
  76. }
  77.  
  78. //display results so far
  79.  
  80. cout << guessString[i] << endl;
  81. cout << "Good guesses: " << goodGuesses << endl;
  82. cout << "Bad guesses: " << badGuesses << endl;
  83.  
  84. guessCount++;
  85.  
  86. //if guessString is now the same as getWord
  87. if(strcmp(guessString, getWord) == 0)
  88. {
  89. incomplete = false; //change the flag variable to stop the loop
  90. break;
  91. }
  92. else if (guessCount == guessCountLimit)
  93. {
  94. break;
  95. }
  96. else
  97. {
  98. //puts the pressure on
  99. cout << "you have " << guessCountLimit - guessCount << " guesses left" << endl;
  100.  
  101. //get next guess
  102. cout << "Guess next letter: ";
  103. cin >> guess;
  104. system("cls");
  105.  
  106. //reset variables
  107. i = 0;
  108. badGuess = true;
  109. }
  110. }
  111.  
  112.  
  113. if(incomplete)//if word is not guessed then output "Sorry you lost. the word was"
  114. {
  115. cout << "Sorry, you lost." << endl;
  116. cout << "The word was: " << getWord << endl;
  117. }
  118. else //if the word is guessed.
  119. {
  120. cout << "You got it!" << endl;
  121. cout << "The word was: " << getWord << endl;
  122. }
  123.  
  124. return 0;
  125. }
Last edited by ~s.o.s~; Apr 29th, 2007 at 2:03 pm. Reason: Fixed code tags.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 8
Reputation: Shad0wHawk is an unknown quantity at this point 
Solved Threads: 0
Shad0wHawk Shad0wHawk is offline Offline
Newbie Poster

Re: C++ Programming issue

 
0
  #16
May 1st, 2007
actually this is not finished. I found out that I forgot to make it case insensitive and also I need to keep track of an alert on repeat guesses.

However the issue I am having is with case insensitivity.

I cannot seem to get it to work. The closest I can get is as follows:
  1. #include <iostream>
  2. #include <cstring> //for functions to manipulate C style strings
  3.  
  4. using namespace std;
  5. bool isCap(char b)
  6. {
  7. if(b > 64 && b < 91)
  8. return true;
  9. return false;
  10. }
  11.  
  12.  
  13. int main()
  14. {
  15.  
  16. char getWord[50];
  17. char guessString[50];
  18. char goodGuesses[99] = {'\0'};
  19. char badGuesses[99] = {'\0'};
  20. char temp;
  21. char guess;
  22. bool badGuess = true;
  23. int guessCount = 0;
  24. int guessCountLimit;
  25. char repeat;
  26. cout << "Please enter a mystery word: ";
  27. cin >> getWord;
  28.  
  29. system("cls");
  30.  
  31. //place one '-' in guessString for every letter in getWord
  32. int i = 0;
  33. while(guessString[i] != '\0')
  34. {
  35. while(getWord[i])
  36. {
  37. guessString[i] = '-';
  38. i++;
  39. }
  40. guessString[i] = '\0';
  41. cout << "The word has " << i << " letters" << "\t";
  42. cout << guessString << endl;
  43. }
  44. //end guessWord with null terminator
  45. guessString[i]='\0';
  46.  
  47.  
  48. //calculate guessCountLimit
  49. guessCountLimit = i * 2;
  50. cout << "You have " << guessCountLimit <<" guesses to start." << endl;
  51. cout << "Guess a letter: ";
  52. cin >> guess;
  53.  
  54. i = 0; //keep track of location in getWord and guessString
  55. int trackGood = 0; //keep track of goodGuesses
  56. int trackBad = 0; //keep track of badGuesses
  57. bool incomplete = true; //means guessString != getWord
  58.  
  59. while(guessCount < guessCountLimit && incomplete)
  60. {
  61. while(getWord[i] != '\0')
  62. {
  63. if(isCap(getWord[i]) && !isCap(guess))
  64. guess = guess - 32;
  65.  
  66. if(!isCap(getWord[i]) && isCap(guess))
  67. guess = guess + 32;
  68.  
  69. if(isCap(getWord[i]) == isCap(guess))
  70. {
  71. guessString[i] = guess; //place correct guess in guessString
  72. badGuess = false; //set flag
  73. }
  74.  
  75. cout << guessString[i];
  76. i++; //advance position in getWord
  77.  
  78. }
  79.  
  80. if(badGuess)
  81. {
  82. badGuesses[trackBad] = guess;
  83. trackBad++; //advance position in badGuesses
  84. }
  85. else
  86. {
  87. goodGuesses[trackGood] = guess;
  88. trackGood++; //advance position in goodGuesses
  89. }
  90.  
  91. //display results so far
  92.  
  93. cout << guessString[i] << endl;
  94. cout << "Good guesses: " << goodGuesses << endl;
  95. cout << "Bad guesses: " << badGuesses << endl;
  96.  
  97. guessCount++;
  98.  
  99. //if guessString is now the same as getWord
  100. if(strcmp(guessString, getWord) == 0)
  101. {
  102. incomplete = false; //change the flag variable to stop the loop
  103. break;
  104. }
  105. else if (guessCount == guessCountLimit)
  106. {
  107. break;
  108. }
  109. else
  110. {
  111. //puts the pressure on
  112. cout << "you have " << guessCountLimit - guessCount << " guesses left" << endl;
  113.  
  114. //get next guess
  115. cout << "Guess next letter: ";
  116. cin >> guess;
  117.  
  118. system("cls");
  119.  
  120. //reset variables
  121. i = 0;
  122. badGuess = true;
  123. }
  124. }
  125.  
  126.  
  127. if(incomplete)//if word is not guessed then output "Sorry you lost. the word was"
  128. {
  129. cout << "Sorry, you lost." << endl;
  130. cout << "The word was: " << getWord << endl;
  131. }
  132. else //if the word is guessed.
  133. {
  134. cout << "You got it!" << endl;
  135. cout << "The word was: " << getWord << endl;
  136. }
  137.  
  138. return 0;
  139. }

However this loads up the ____ with all the same letter for instance:
When the mystery word is "Antique" you guess "A"
and it outputs "Aaaaaaa" then you guess "n" it outputs "nnnnnnn"
and so on.

I need it to be case insensitive and at the same time I need to output characters EXACTLY as the player enters them. So if they put in AnTiQuE it comes out as such even if the word was "Antique" They need to be able to use any combination of capital and lowercase letters and have it return correct as soon as it it guessed.

Further the code above for does not return a "correct" when the word is guessed, I think it has to do with Ascii values not matching.

Any help or guidance on how to acheive the desired final results would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 30
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: C++ Programming issue

 
0
  #17
May 1st, 2007
this may require you to check your capital letters in a separate array...

my idea of a simple process to do this is the next one:

1.- create an array that includes all capital letters.

2.-inside a loop, compare all of the letters in the string to both arrays (capital and non-capital letters), and convert all letters in the string to non-capital letters, so that you won't have any trouble further...

3.- to make it trouble proof, compare all of the users inputs to both arrays in a loop, and convert all inputs into non-capital letters

4.- compare the input you just converted with the word you want the user to find...
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 8
Reputation: Shad0wHawk is an unknown quantity at this point 
Solved Threads: 0
Shad0wHawk Shad0wHawk is offline Offline
Newbie Poster

Re: C++ Programming issue

 
0
  #18
May 2nd, 2007
Originally Posted by Nichito View Post
this may require you to check your capital letters in a separate array...

my idea of a simple process to do this is the next one:

1.- create an array that includes all capital letters.

2.-inside a loop, compare all of the letters in the string to both arrays (capital and non-capital letters), and convert all letters in the string to non-capital letters, so that you won't have any trouble further...

3.- to make it trouble proof, compare all of the users inputs to both arrays in a loop, and convert all inputs into non-capital letters

4.- compare the input you just converted with the word you want the user to find...
actually it was much simpler than that. the issue was here
  1. while(getWord[i] != '\0')
  2. {
  3. if(isCap(getWord[i]) && !isCap(guess))
  4. guess = guess - 32;
  5.  
  6. if(!isCap(getWord[i]) && isCap(guess))
  7. guess = guess + 32;
  8.  
  9. [B]if(isCap(getWord[i]) == isCap(guess)) //changed to if(getWord[i] == guess) and it worked.
  10. [/B] {
  11. guessString[i] = guess; //place correct guess in guessString
  12. badGuess = false; //set flag
  13. }
  14.  
  15. cout << guessString[i];
  16. i++; //advance position in getWord
  17.  
  18. }
Last edited by Shad0wHawk; May 2nd, 2007 at 6:27 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 3286 | Replies: 17
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC