Guessing game loop problems

I've been working on this game to guess the secret word, but i've had more problems than success. I'm rusty on my C++ and never was very fluent and I can't figure out how to get the game working right. What I mean is the program needs to go through a loop 5 times or until you guess the secret word. I can't figure out the loop part it's been giving me trouble for days now. With the code that i have right now it will only go thought the loop 2 times before going into an ifinite loop. If anybody could help we figure out how to make this work would be great. Thank alot.

#include<iostream>
#include<string>

using namespace std;

int main()
{
int count= 1;
string SECRET_WORD = "binary";
string SECRET_GUESS;

cout << "Please guess the secret word!" << endl;
cin >> SECRET_GUESS;

do
{
cout << "Please guess the secret word!" << endl;
cin >> SECRET_GUESS;
++count;
}
while(count <= 5 || SECRET_WORD == SECRET_GUESS)

cout << "Thank you for playing" << endl;

cout << "Correct the secret word was " << SECRET_WORD << endl;

system("pause");
return 0;
}
Last edited on
You should remove the two line before the loop and change == to != in the while condition.
After the do-while you need a semicolon
OK. I changed the lines of code and now it won't exit the loop or accept the "binary" as the secret word. I'm sorry, I really suck at loops in C++. Here is my code now after the changes. The loop needs to go 5 times or until and Guess the right word which is "binary". Thanks, again.

#include<iostream>
#include<string>

using namespace std;

int main()
{
int count= 1;
string SECRET_WORD = "binary";
string SECRET_GUESS;

do
{
cout << "Please guess the secret word!" << endl;
cin >> SECRET_GUESS;
++count;
}
while(count <= 5 || SECRET_WORD != SECRET_GUESS);

cout << "Thank you for playing" << endl;

cout << "Correct the secret word was " << SECRET_WORD << endl;

system("pause");
return 0;
}
Your condition has an '||', you want an '&&'. Also, try not using system("pause"), and using std::getline() with strings instead of using >>.
Thanks alot. I'm kinda new to C++ and I don't know other than what I've read in my text books. But thanks so much again to everybody.
I'm sorry to bother again, but how do make the program display "Thank you for playing" only when you didn't guess the correct word and ran out of guesses. Right now the program displays "Thank you for playing" after I guess the right word, but i don't want it to do that. And it says "Correct the secret word was binary" after the loops exits even if you didn't guess the right word. I know that the problem is that the cout if after the loop and will display no matter what, but how do i fix it so it only display a specific message after the loop. Like if i didn't guess the word it should onyl display "Thank you for playing" and if i did guess the right word it should onyl display "Correct the secret word was binary". I know i'm a bother and i'm sorry I just can't figure this out for some reason. Thanks alot.

Sorry I forgot to add code, sorry.

#include<iostream>
#include<string>

using namespace std;

int main()
{
int count= 1;
string SECRET_WORD = "binary";
string SECRET_GUESS;

do
{
cout << "Please guess the secret word!" << endl;
cin >> SECRET_GUESS;
++count;
}
while(count <= 5 && SECRET_WORD != SECRET_GUESS);

cout << "Thank you for playing" << endl;

cout << "Correct the secret word was " << SECRET_WORD << endl;

system("pause");
return 0;
}
Last edited on
Put the last two cout lines in an if checking whether the word wasn't found
Thanks bunches.
Topic archived. No new replies allowed.