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.

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
#include <iostream>
#include <string>
#include <algorithm>


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){
                                   cout << 	"No way, my number is smaller. " 
				"You have still " << lifes << " life/s.\n";
                                   cin>>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
1
2
 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.

1
2
3
4
5
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:

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
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <ctime> // 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){
                                   cout << 	"No way, my number is smaller. "
				"You have still " << lifes << " life/s.\n";
                                   cin>>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.