How to restart the program after loop?

saugat (33)
I have been trying for hours, this is a guessing game and its suppose to have 4 rounds the program is supposed to restart(expect for inputting the start/end value for the guess) until (round<4) how can i make it do that and after every round the range of the guess should be the same but the guess itself should be different.

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
96
97
98
99
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;



int main()
{
    srand(time(0));


    int start;
    int end;

    cout<<"Enter the starting value for what your guess can be between ";
    cin>>start;
    cout<<"Enter the ending value for your guess can be between ";
    cin>>end;




    int range;
    range=(start-end);
    int secretNumber = (rand() % range) + start;

    int tries = 0;
    int round;
    int guess;





    range=(start-end);


do {

    do
    {




        cout << "Enter a guess for game ";
        cin >> guess;
        ++tries;



          if (guess > secretNumber)
            {
                cout << "Too high!" << endl << endl;
            }

            if (guess < secretNumber)
            {
                cout << "Too low!" << endl << endl;
            }
             if (guess == secretNumber)



    {

        cout << "You win! You got it in " << tries << " tries!" << endl;



    }

    ++round;

   } while (round<4);

    } while (guess != secretNumber);









}









saugat (33)
bump
Luc Lieber (911)
You're on the right track.

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

Get values for the range

do
{

  Generate a new secret number
  
  do
  {

    Get the user's guess

    if ( the guess is too high or too low )
    {

      tell them about it

    }

  } while ( the user's guess is incorrect )

  tell the user how many guesses it took them

} while ( you haven't played 4 rounds yet )
 
saugat (33)
I get everything else but how exactly am i suppose to generate a new secret number, i can't make a new variable. I think i am supposed to use arrays.
Raezzor (230)
Simply call the rand function again. You already seeded it so you don't need to do srand() again, but rand() will generate a new random number.

secretNumber = (rand() % range) + start;
Volatile Pulse (1329)
It's also possible to just use a do/while loop again to encapsulate your entire code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char response;
do {
cout<<"Enter the starting value for what your guess can be between ";
    cin>>start;
    cout<<"Enter the ending value for your guess can be between ";
    cin>>end;


// ...

   } while (round<4);

    } while (guess != secretNumber);
cout << "Would you like to play again? (Y/N)";
cin >> response;
} while (response == 'Y' || response == 'y');
saugat (33)
Still not getting a new number.
saugat (33)
Got it even though i had to do srand() again for some reason.
Volatile Pulse (1329)
Repost your final code and we'll show you why. You only need to seed the randomizer once. The odds of getting a number back to back isn't impossible, but depending on your seed is likely.

If you used what I provided above, it requires minor additions to your code and wouldn't require another call to srand.
Registered users can post here. Sign in or register to post.