Problem in Code (Guessing-Game)

I am trying to create a guessing game. Everytime I execute, it only says the "couts." It lets me input numbers/letters, but, then it always says, "Would you like to play again? Type, 'G,' for, 'Go,' or, 'N,' for 'No.'" Please help me fix 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main(void)
{
//New Project (Number Guessing Game)
   
    
    while(true)
    {
        srand(time(NULL));
    

        
        int randomnumber = rand() % 999 + 3;
        int tries = 0;
        int guess;
        char answer;
        int highestvalue = 100;
        string end;
        
        while(true) {
    
            cout << "Type in a number from 1 to 100. You have " << 20 - tries << " tries left." << endl;
            cin >> guess;
            cin.ignore();
        
                if(guess > randomnumber)
                {
                    cout << "The number you guessed was too high! Try again!" << endl;
                }
        
                else if(guess < randomnumber)
                {
                    cout << "The number you guessed was too low! Try again!" << endl;
                }
                else
                {
                    break;
                }
        
                tries++;
            
                if(guess > highestvalue)
                {
                    cout << "I told you the highest number was 100!" << endl;
                 
                }
       
                tries++;
        
                if(tries >= 20)
                {
                    cout << "You ran out of tries!" << endl;
                }
                else
                {
                    cout << "Awesome! You guessed the number!" << endl;
                    cout << "You got the right number in " << tries << " tries!" << endl;
                }
        
                while(true)
                {
                    cout << "Would you like to play again? Type, 'G,' for, 'Go,'  or, 'N,' for 'No.'" << endl;
                    cin >> answer;
                    cin.ignore();
                    
                    if(answer == 'N' || answer == 'n' || answer == 'G' || answer == 'g')
                    {
                        break;
                    }
                    else
                    {
                        cout << "Please type, 'Yes,' or, 'No.'" << endl;
                    }
                }
		
                if(answer == 'n' || answer == 'N')
                {
                    cout << "See you later!" << endl;
                    break;
                }
        
        break;
   
        }
        
        return 0;
    }

}


Thanks.
Last edited on
If the number is only supposed to be from 1 to 100, the randomnumber assignment here needs to change - right now this is going to assign values from 3 to 1001.

int randomnumber = rand() % 999 + 3;


This block is getting run even if the correct number hasn't been guessed yet. So the "Awesome.." message gets printed out even if the wrong number is guessed and there are still tries left.

1
2
3
4
5
6
7
8
9
                if(tries >= 20)
                {
                    cout << "You ran out of tries!" << endl;
                }
                else
                {
                    cout << "Awesome! You guessed the number!" << endl;
                    cout << "You got the right number in " << tries << " tries!" << endl;
                }


computer number is 38 //I just added in output of the computer number so I knew what it was
Type in a number from 1 to 100. You have 20 tries left.
15
The number you guessed was too low! Try again!
Awesome! You guessed the number!
You got the right number in 2 tries!
Would you like to play again? Type, 'G,' for, 'Go,'  or, 'N,' for 'No.'
n
See you later!



I might try breaking the code into separate functions though I'm not sure if the assignment allows you to do that.
This wasn't an assignment. It was just goof-around code. :P Thanks dude for your help!
Topic archived. No new replies allowed.