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.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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);
}
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 )
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');
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.