Help with Random number game.

Okay well my assignment is to write a program that generates a random number from 0-100, allows the user to guess the number, Lets the user pick three different difficulty levels:
easy mode - until they get it
medium mode - 10 guesses
hard mode - 5 guesses
Tells the user if they are too high or too low and how many guesses are left. And lets the user play again.

Here is my code:
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <iomanip>


using namespace std;

int main(int argc, char *argv[])
{


char choice = ' ';
unsigned int seed = time(0);
srand(seed);
int random = rand() % 100;
int guess = 0;
const int H_MAXGUESS = 5;
const int M_MAXGUESS = 10;
char again = ' ';

do
{
cout << "RANDOM NUMBER GUESSING GAME" << endl;
cout << " " << endl;
cout << "You have to guess what number i'm thinking between 0-100" << endl;
cout << "Pick difficulty level" << endl;
cout << "Easy: Unlimited guesses" << endl;
cout << "Medium: 10 guesses" << endl;
cout << "Hard: 5 guesses" << endl;
cout << " " << endl;
cout << "type E for Easy, M for Medium or H for Hard." << endl;
cin >> choice;

switch(toupper(choice))
{
case 'E': cout << "You have chosen Easy mode. You have unlimited guesses. Good luck." << endl;
for(int i = 0; i < 2; i)
{
cout << "Please type in your guess. Remember the number is 0-100" << endl;
while(!(cin >> guess))
{
cin.clear();
cin.ignore(100,'\n');
cout << "please enter a number only: ";
}
if(guess == random)
{
cout << "Congratulations! You've won!" << endl;
i = 3;
}
else if(guess > random)
cout << "Your guess is too high." << endl;

else if(guess < random)
cout << "Your guess is too low." << endl;
}
break;

case 'M': cout << "You have chosen Medium mode. You have 10 guesses. Good luck." << endl;
for(int i = 0; i < 10; i++)
{
cout << "Please type your guess. Remember the number is 0-100" << endl;
while(!(cin >> guess))
{
cin.clear();
cin.ignore(100,'\n');
cout << "please enter a number only: ";
}
if(guess == random)
{
cout << "Congratulations! You've won!" << endl;
i = 11;
}
else if(guess > random)
{
cout << "Your guess is too high." << endl;
cout << "You have " << (M_MAXGUESS - 1) - i << " guesses left" << endl;
}
else if(guess < random)
{
cout << "Your guess is too low." << endl;
cout << "You have " << (M_MAXGUESS - 1) - i << " guesses left" << endl;
}
}
break;

case 'H': cout << "You have chosen Hard mode. You have 5 guesses. Good luck." << endl;
for(int i = 0; i < 5; i++)
{
cout << "Please type your guess. Remember the number is 0-100" << endl;
while(!(cin >> guess))
{
cin.clear();
cin.ignore(100,'\n');
cout << "please enter a number only: ";
}
if(guess == random)
{
cout << "Congratulations! You've won!" << endl;
i = 6;
}
else if(guess > random)
{
cout << "Your guess is too high." << endl;
cout << "You have " << (H_MAXGUESS - 1) - i << " guesses left" << endl;
}
else if(guess < random)
{
cout << "Your guess is too low." << endl;
cout << "You have " << (H_MAXGUESS - 1) - i << " guesses left" << endl;
}
}
break;

default: cout << "Error. Please type \"E\" or \"M\" or \"H\" to select your difficulty level." << endl;
}
cout << "Do you wan't to play again?" << endl;
cin >> again;
} while (again == 'Y' || again == 'y');

cout << "ok bye" << endl;


system("PAUSE");
return EXIT_SUCCESS;
}



The problem is that if they chose to play again, the number is going to be the same as before, it doesn't generate a new random number. Works just fine if you close and reopen the program. How do i fix this?
Last edited on
I think you need to move the following line into do/while(again) loop.

int random = rand() % 100;
thank you it works now :)
Topic archived. No new replies allowed.