| | |
C++ Programming issue
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> //for functions to manipulate C style strings using namespace std; int main() { char getWord[50]; char guessString[50]; char goodGuesses[99] = {'\0'}; char badGuesses[99] = {'\0'}; char guess; bool badGuess; int guessCount = 0; int guessCountLimit; cout << "Please enter a word" << endl; cin >> getWord; //place one '-' in guessString for every letter in getWord int i = 0; while(getWord[i]) { guessString[i] = '-'; i++; } //end guessWord with null terminator guessString[i] = '\0'; //calculate guessCountLimit guessCountLimit = i * 2; cout << "Guess a letter: "; cin >> guess; i = 0; //keep track of where you are in getWord and guessString int j = 0; //keep track of goodGuesses int k = 0; //keep track of badGuesses bool incomplete = true; //means guessString != getWord while(guessCount <= guessCountLimit && incomplete) { while(getWord[i] != '\0') { if(getWord[i] == guess) { guessString[i] = guess; //place correct guess in guessString badGuess = false; //set flag } i++; //advance position in getWord } if(badGuess) { badGuesses[k] = guess; k++; //advance position in badGuesses } else { goodGuesses[j] = guess; j++; //advance position in goodGuesses } //display results so far cout << "Good guesses: " << goodGuesses << endl; cout << "Bad guesses: " << badGuesses << endl; guessCount++; //if guessString is now the same as getWord if(strcmp(guessString, getWord) == 0) { incomplete = false; //change the flag variable to stop the loop } else { //put the pressure on cout << "you have " << guessCountLimit - guessCount << " guesses left" << endl; //get next guess cout << "Guess next letter: "; cin >> guess; //reset variables i = 0; badGuess = true; } } //now why did the loop stop if(incomplete) { cout << "Sorry, you lost" << endl; } else { cout << "You got it!" << endl; cout << "The word was: " << guessString << endl; } }
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...
i don't know... maybe it can make things a bit more simple...
-->sometimes i wanna take my toaster in a bath<-- •
•
•
•
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...
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?
get it?
-->sometimes i wanna take my toaster in a bath<-- •
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
Thanks to all of you for your help. I got it!!!
Here is the final code...
Here is the final code...
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> //for functions to manipulate C style strings using namespace std; int main() { char getWord[50]; char guessString[50]; char goodGuesses[99] = {'\0'}; char badGuesses[99] = {'\0'}; char guess; bool badGuess = true; int guessCount = 0; int guessCountLimit; cout << "Please enter a word: "; cin >> getWord; system("cls"); //place one '-' in guessString for every letter in getWord int i = 0; while(guessString[i] != '\0') { while(getWord[i]) { guessString[i] = '-'; i++; } guessString[i] = '\0'; cout << "The word has " << i << " letters" << "\t"; cout << guessString << endl; } //end guessWord with null terminator guessString[i]='\0'; //calculate guessCountLimit guessCountLimit = i * 2; cout << "You have " << guessCountLimit <<" guesses to start." << endl; cout << "Guess a letter: "; cin >> guess; i = 0; //keep track of location in getWord and guessString int trackGood = 0; //keep track of goodGuesses int trackBad = 0; //keep track of badGuesses bool incomplete = true; //means guessString != getWord while(guessCount < guessCountLimit && incomplete) { while(getWord[i] != '\0') { if(getWord[i] == guess) { guessString[i] = guess; //place correct guess in guessString badGuess = false; //set flag } cout << guessString[i]; i++; //advance position in getWord } if(badGuess) { badGuesses[trackBad] = guess; trackBad++; //advance position in badGuesses } else { goodGuesses[trackGood] = guess; trackGood++; //advance position in goodGuesses } //display results so far cout << guessString[i] << endl; cout << "Good guesses: " << goodGuesses << endl; cout << "Bad guesses: " << badGuesses << endl; guessCount++; //if guessString is now the same as getWord if(strcmp(guessString, getWord) == 0) { incomplete = false; //change the flag variable to stop the loop break; } else if (guessCount == guessCountLimit) { break; } else { //puts the pressure on cout << "you have " << guessCountLimit - guessCount << " guesses left" << endl; //get next guess cout << "Guess next letter: "; cin >> guess; system("cls"); //reset variables i = 0; badGuess = true; } } if(incomplete)//if word is not guessed then output "Sorry you lost. the word was" { cout << "Sorry, you lost." << endl; cout << "The word was: " << getWord << endl; } else //if the word is guessed. { cout << "You got it!" << endl; cout << "The word was: " << getWord << endl; } return 0; }
Last edited by ~s.o.s~; Apr 29th, 2007 at 2:03 pm. Reason: Fixed code tags.
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
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:
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.
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:
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> //for functions to manipulate C style strings using namespace std; bool isCap(char b) { if(b > 64 && b < 91) return true; return false; } int main() { char getWord[50]; char guessString[50]; char goodGuesses[99] = {'\0'}; char badGuesses[99] = {'\0'}; char temp; char guess; bool badGuess = true; int guessCount = 0; int guessCountLimit; char repeat; cout << "Please enter a mystery word: "; cin >> getWord; system("cls"); //place one '-' in guessString for every letter in getWord int i = 0; while(guessString[i] != '\0') { while(getWord[i]) { guessString[i] = '-'; i++; } guessString[i] = '\0'; cout << "The word has " << i << " letters" << "\t"; cout << guessString << endl; } //end guessWord with null terminator guessString[i]='\0'; //calculate guessCountLimit guessCountLimit = i * 2; cout << "You have " << guessCountLimit <<" guesses to start." << endl; cout << "Guess a letter: "; cin >> guess; i = 0; //keep track of location in getWord and guessString int trackGood = 0; //keep track of goodGuesses int trackBad = 0; //keep track of badGuesses bool incomplete = true; //means guessString != getWord while(guessCount < guessCountLimit && incomplete) { while(getWord[i] != '\0') { if(isCap(getWord[i]) && !isCap(guess)) guess = guess - 32; if(!isCap(getWord[i]) && isCap(guess)) guess = guess + 32; if(isCap(getWord[i]) == isCap(guess)) { guessString[i] = guess; //place correct guess in guessString badGuess = false; //set flag } cout << guessString[i]; i++; //advance position in getWord } if(badGuess) { badGuesses[trackBad] = guess; trackBad++; //advance position in badGuesses } else { goodGuesses[trackGood] = guess; trackGood++; //advance position in goodGuesses } //display results so far cout << guessString[i] << endl; cout << "Good guesses: " << goodGuesses << endl; cout << "Bad guesses: " << badGuesses << endl; guessCount++; //if guessString is now the same as getWord if(strcmp(guessString, getWord) == 0) { incomplete = false; //change the flag variable to stop the loop break; } else if (guessCount == guessCountLimit) { break; } else { //puts the pressure on cout << "you have " << guessCountLimit - guessCount << " guesses left" << endl; //get next guess cout << "Guess next letter: "; cin >> guess; system("cls"); //reset variables i = 0; badGuess = true; } } if(incomplete)//if word is not guessed then output "Sorry you lost. the word was" { cout << "Sorry, you lost." << endl; cout << "The word was: " << getWord << endl; } else //if the word is guessed. { cout << "You got it!" << endl; cout << "The word was: " << getWord << endl; } return 0; }
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.
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...
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<-- •
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
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...
c++ Syntax (Toggle Plain Text)
while(getWord[i] != '\0') { if(isCap(getWord[i]) && !isCap(guess)) guess = guess - 32; if(!isCap(getWord[i]) && isCap(guess)) guess = guess + 32; [B]if(isCap(getWord[i]) == isCap(guess)) //changed to if(getWord[i] == guess) and it worked. [/B] { guessString[i] = guess; //place correct guess in guessString badGuess = false; //set flag } cout << guessString[i]; i++; //advance position in getWord }
Last edited by Shad0wHawk; May 2nd, 2007 at 6:27 pm.
![]() |
Similar Threads
- General Programming issue (Computer Science)
- problem in hotmail (Java)
Other Threads in the C++ Forum
- Previous Thread: isinvalidInt() ??????
- Next Thread: C++ file to array
Views: 3286 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






