My random number generator doesnt work

In this code, the output for whatever number you put in is "smaller smaller" How can I 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
int main(int argc, char *argv[])
{
int choice;
string username, i;
string susername;
string password;
string Spassword;
int life, guess, random;
random = rand() % 10 + 1;
life = 10;
cout<<"This is a text based game. Always answer Yes or No questions with Y or N\n";
cout<<"Guess a number between 1 and 10,\n";
cout<<"and with every guess, your life decreases by one.\n";
cout<< "\n\nPick a number between 1 and 10.\n";
    cout<< "You have " << life <<" lives.\n";
    cin>> guess;
    while (guess < random)
    {
            cout<< "Bigger!\n";
            cin.get();
            }
    while (guess > random)
    { 
            cout<< "Smaller!\n";
            cin.get();
    }
    while (guess > 10)
    {
            cout<< "I said 1 - 100!\n";
            cout<< "You neglected to follow directions. YOU LOSE.\n";
            life = 0;
            cin.get();
            return 0;
            }
    while (guess < 0)
    {
            cout<< "I said 1 - 100!\n";
            cout<< "You neglected to follow directions. YOU LOSE.\n";
            life=0;
            cin.get();
            return 0;
            }
            
 if (guess == random)
{
     cout<<"GOOD JOB :D\n";
     cout<<" ____________\n";
     cout<<"|            |\n";
     cout<<"|            |\n";
     cout<<"|  SUCCESS   |\n";
     cout<<"|            |\n";
     cout<<"|____________|\n";
     std::cin >> i;
     cout<<"Good job.\n";
     pbg();
                                                  
                          }                
 if (choice == 2)
 {
            cout<<"Wow! You managed to lose the game, it hasnt even started!";
            cin.ignore();
            cin.ignore();
            return 0;
            }
Do you know what while does? It seems you confused it with if.
See here: http://www.cplusplus.com/doc/tutorial/control/
If I do a if loop, I cant get the person to be able to go more than once.
There is no such thing as in "if loop." What you want is those statements to be if statements, and have a larger while loop around the entire program (what you want to repeat). Obviously you don't want to repeat the text over and and over again, so don't put a while loop around it.
I seriously recommend you re-think the structure of your program, it is currently a complete mess. Can't even see it working for what you want.

Sounds like you should have a while loop around your whole program in main and use if conditions amongst it to accommodate all the various inputs.

A fraction the size of your code, will work, it also does a bit more in the way of logic.
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
int main()
{
   const int MAX_TRIES = 5;
   int tries = 0, guess, random = 1 + rand() % 10;

   cout << "Guess a number game, type 0 to exit\n";

   do
   {
      cout << "\nPick a number between 1 and 10.\n";
      cout << "You have " << life <<" lives.\n";
      cin >> guess;

      if (guess == random)
         cout <<"GOOD JOB :D\n";
      else if (guess > 10 || guess < 0)
         cout << "I said 1 - 100!\n";
      else if (guess < random)
         cout << "Bigger!\n";
      else if (guess > random)
         cout << "Smaller!\n";
                              
      if (++tries == MAX_TRIES)
         cout << "You lost.";
   } while(guess != 0 && guess != random && tries < MAX_TRIES);
}
Last edited on
Topic archived. No new replies allowed.