first time using c++

Hello at the moment I'm learning c++ and I've been working on a guessing game as practice but I need help. So far here is what i have made, but what I would like to know is how allow the player to be able to guess again if he/she choose a number that is too low or too high. Thanks.



#include <iostream>

using namespace std;

int main()
{
string player1;
string player2;
int anumber;
int bnumber;
int x;
int y;
cout << "Player 1 Enter Your Name" << endl;
cin >> player1;
cout << "Player 2 Enter Your Name" << endl;
cin >> player2;
cout << player1 << ", Enter Your Number" << endl;
cin >> anumber;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << player2 << ", Enter Your Number" << endl;
cin >> bnumber;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << player1 << ", Try to Guess " << player2 << "'s number" << endl;
cin >> x;
if(x==bnumber){
cout << "You Win!" << endl;
}
if(x>bnumber){
cout << "Too High" << endl;
}
if(x<bnumber){
cout << "Too Low" << endl;
}
cout << player2 << ", Try to Guess " << player1 << "'s number" << endl;
cin >> y;
if(y==anumber){
cout << "You Win!" << endl;
}
if(y>anumber){
cout << "Too High" << endl;
}
if(y<anumber){
cout << "Too Low" << endl;
}
return 0;
}
You could use a loop to accomplish this. The code was altered, so I will explain what is changed in an edit.

EDIT: I began by enclosing the following in two curly brackets, thus creating a compound statement: the conditional statements (if statements) and instructions for user (guess the number of <player name>). I changed the if statements to a chain of if-else statements. If you hand't, then every condition would be evaluated. I added two break statements to the if statements that checked to see if a player had one. If a user guesses the correct number, the loop will break and the program will end once the return statement is reached.

EDIT2: I would normally use the while loop's condition to check to see if the user had guessed the correct number, but that introduces other problems in this situation. If the first player were to guess player 2's number, then the loop wouldn't break until the condition was evaluated. The while loop's condition is evaluated at the top of the compound statement. The use of a do-while loop wouldn't help eaither. Using the conditions in the both of these loops would still allow the second player to guess player one's number, even if player 1 had already correctly guessed player two's number.

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
#include <iostream>
 
using namespace std;
 
int main()
{
    string player1;
        string player2;
        int anumber;
        int bnumber;
        int x;
        int y;
        cout << "Player 1 Enter Your Name" << endl;
        cin >> player1;
        cout << "Player 2 Enter Your Name" << endl;
        cin >> player2;
        cout << player1 << ", Enter Your Number" << endl;
        cin >> anumber;
        cout << player2 << ", Enter Your Number" << endl;
        cin >> bnumber;
        while(true) {
                cout << player1 << ", Try to Guess " << player2 << "'s number" << endl;
                cin >> x;
                if(x==bnumber){
                        cout << "You Win!" << endl;
                        break;
                }
                else if(x>bnumber){
                        cout << "Too High" << endl;
                }
                else if(x<bnumber){
                        cout << "Too Low" << endl;
                }
                cout << player2 << ", Try to Guess " << player1 << "'s number" << endl;
                cin >> y;
                if(y==anumber){
                        cout << "You Win!" << endl;
                        break;
                }
                else if(y>anumber){
                        cout << "Too High" << endl;
                }
                else if(y<anumber){
                        cout << "Too Low" << endl;
                }
        }
        return 0;
}


Run: http://ideone.com/MMq0L7
Last edited on
To continually run the same code, we use things called Loops. These basically allow us to control the flow of our program.

There are 3 main types of loop. for loop, while loops and do while loops. All of these have been explained far better than I could do in this thread, a simple google will explain them all.

As for your problem, personally I would use a while loop.

1
2
3
4
5
while(x != bnumber)
{
   cout << player1 << ", Try to Guess " << player2 << "'s number" << endl;
cin >> x;
}



As a side note, your giant list of endl's is unnerving to me. You could also achieve this with a nice loop.
1
2
3
4
5
string whiteSpace = "";
for(int i=0; i < 40; i++)
{
  whiteSpace +="\n";
}



Then you can simply write:


1
2
3
4
5
6
7
8
cout << "Player 1 Enter Your Name" << endl;
cin >> player1;
cout << "Player 2 Enter Your Name" << endl;
cin >> player2;
cout << player1 << ", Enter Your Number" << endl;
cin >> anumber;
cout<<whiteSpace;
..etc
Awesome, thanks for the help!
Topic archived. No new replies allowed.