### My abnormal guess game

Hi there, it's my first post here.
I'm new in C++ some would even say that I'm a complete...

I was trying differend things, started with simple console programming and
ended at getting some idea about WinAPI. I couldn't find any good solution
to make further progress in learning. I went back where I started.
So yeah, that's my main problem.

I was trying to make my old guess game more complex,
and it looks like I'm not even able to do this.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include #include #include using namespace std; int main() { string input; bool won=false, con; int right, user; cout << "PC: Hi there, let's play a game.\n " "I take a number between 1 and 100, you gonna guess it.\n\n " "To Play, let me know what's your name.\n " "If you want to exit answer me with a NO.\n\n "; cin >> input; cin.ignore(); std::transform(input.begin(), input.end(),input.begin(), ::toupper); while(con){ cout << " Hi " << input << "." << endl; srand (time(NULL)); right=rand() % 100; cout << right; int lifes=5; cout << " So let's start! " "I have choosen my number already, now guess it.\n" "You have 5 lifes.\n" << endl; cin >> user; cin.ignore(); while(lifes>=1){ if(input=="NO"){!con;} --lifes; if(right>user; } else if(right>user){ cout << "Nope, my number is higher. " "You have still " << lifes << " life/s.\n"; cin>>user; } else{ cout << "Nice one, you won!"; won=true; } cin.ignore(); } cout << "Game Over! Press enter to restart or type NO to exit."; cin.get(); } return 0; }``````

That's my code. It is far from "clean", because of many tests,
but I hope you will get my idea. Maybe any kind person could help me out.
Last edited on
consider moving this
 `` `` ``if(input=="NO"){!con;} ``

after
 ``12`` `````` cout << "Game Over! Press enter to restart or type NO to exit."; cin.get();``````

but before the
 `` `` ``}``

Also try a break statement in the else block where the user guesses the correct number.

 ``12345`` ``````else{ cout << "Nice one, you won!"; won=true; break; }``````

This will cause program flow to exit the while loop and move into line 67 where you prompt the user for a new game or not.
Thank You, It seems work fine.

One more question.
Is there any way to make the program to exit on typing "NO" for example at 3rd try?
Because usually on typing an char into int field program will fall into endless loop.
The "NO" option doesn't work at all. Anyone, help?
on line 29 you use `user`, but on line 33 you check `input` which will not change after line 17 -> use `user`

Furtther more on line 33 `{!con;}` will do nothing. You need to assign it: `{con = false;}` and you need to break the 'lifes' loop:

`if(user=="NO") {con = false; break;}`

and put line 34 until the end of the loop into the else branch of the `if(user=="NO")`
Your solution is pretty logic for me, but not for the compiler... :/

 `35 41 [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] `
Last edited on
Your problem is that you want both: string and int which doesn't work. For this you may use stringstream:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include #include #include #include // for time(NULL) using namespace std; int main() { string input; bool won=false, con; int right, user; cout << "PC: Hi there, let's play a game.\n " "I take a number between 1 and 100, you gonna guess it.\n\n " "To Play, let me know what's your name.\n " "If you want to exit answer me with a NO.\n\n "; cin >> input; cin.ignore(); std::transform(input.begin(), input.end(),input.begin(), ::toupper); while(con){ cout << " Hi " << input << "." << endl; srand (time(NULL)); right=rand() % 100; cout << right; int lifes=5; cout << " So let's start! " "I have choosen my number already, now guess it.\n" "You have 5 lifes.\n" << endl; cin >> input; cin.ignore(); stringstream ss(input); if(ss >> user) { while(lifes>=1){ if(input=="NO"){!con;} --lifes; if(right>user; } else if(right>user){ cout << "Nope, my number is higher. " "You have still " << lifes << " life/s.\n"; cin>>user; } else{ cout << "Nice one, you won!"; won=true; } cin.ignore(); } cout << "Game Over! Press enter to restart or type NO to exit."; cin >> input; } if(input=="NO"){con=false;} } return 0; }``````

Now for the first guess if you type NO the game ends.
Topic archived. No new replies allowed.