Word Scrambler, works but not 100% could use some pointers
Nov 29, 2016 at 10:01pm UTC
So I decided to make a little word scrambler as one of my first little starter projects.
It works, but I keep missing something that will recognize when the user guesses the correct word. the script is recognizing the scrambled word as the right word.
i know whats wrong in the code i think, just don't know how to fix it. maybe its the frustration thats preventing me from figuring it out lol.
i was going to take the easy way out by just having the user enter the word twice (taking place of the hint) but decided to look for better options.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
cout << "Enter word to be scrambled here: " ;
cin >> str;
random_shuffle(str.begin(), str.end());
cout << "Enter a hint if you wish.\n" ;
cin >> hint;
cout << endl << endl << endl << endl << endl << endl << endl <<endl;
cout << "The word to be guessed is: " << str << '\n' ;
cout << "Your hint is\n" << hint << '\n' ;
cout << "You have 3 attempts, Enter your first guess here:\n" << endl;
cin >> guess1;
{
if (guess1 != str) {
cout << "Sorry Guess Again :( \n" ;
cin >> guess2;
}
if (guess1 == str) {
cout << "Congrats\n" ;
return 0;
}
if (guess2 != str) {
cout << "Sorry \n" ;
cin >> guess3;
}
if (guess2 == str) {
cout << "Congrats \n" ;
return 0;
}
if (guess3 != str) {
cout << "Sorry \n" ;
cout << "The word was: " << str << endl;
}
if (guess3 == str) {
cout << "Congrats" << endl;
return 0;
}
}
}
Last edited on Nov 29, 2016 at 10:03pm UTC
Nov 29, 2016 at 11:19pm UTC
Because you check if the scrambled word is equal to guess . You scramble str on line 5. You should store the original word and the scrambled word in 2 different strings.
Nov 30, 2016 at 12:03am UTC
thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
int main()
{
string originalword,str,hint,guess1,guess2,guess3;
cout << "Enter word to be scrambled here: " ;
cin >> str;
originalword = str;
random_shuffle(str.begin(), str.end());
cout << "Enter a hint if you wish.\n" ;
cin >> hint;
cout << endl << endl << endl << endl << endl << endl << endl <<endl;
cout << "The word to be guessed is: " << str << '\n' ;
cout << "Your hint is\n" << hint << '\n' ;
cout << "You have 3 attempts, Enter your first guess here:\n" << endl;
cin >> guess1;
{
if (guess1 != originalword) {
cout << "Sorry Guess Again :( \n" ;
cin >> guess2;
}
if (guess1 == originalword) {
cout << "Congrats\n" ;
return 0;
}
if (guess2 != originalword) {
cout << "Sorry \n" ;
cin >> guess3;
}
if (guess2 == originalword) {
cout << "Congrats \n" ;
return 0;
}
if (guess3 != originalword) {
cout << "Sorry \n" ;
cout << "The word was: " << str << endl;
}
if (guess3 == originalword) {
cout << "Congrats" << endl;
return 0;
}
}
}
Topic archived. No new replies allowed.