int main()
{
//declare variables
int randomNumber = 0;
int numberGuess = 0;
int attempts = 0;
int startTime = 0;
int endTime = 0;
int totalSecond = 0;
int upperBound = 0;
char gameType = ' ';
char again = 'Y';
while(again == 'Y' || again == 'y')
{
cout <<" Game Type Enter B for Beginner,I for Intermediate, and A for Advanced:";
cin >> gameType;
gameType=toupper(gameType);
if(gameType == 'B')
{
cout <<" Guess a number from 1 through 10:";
upperBound = 10;
}
else if( gameType == 'I')
{
cout <<" Guess a number from 1 through 100:";
upperBound = 100;
}
else if( gameType == 'A')
{
cout <<" Guess a number from 1 through 1000:";
upperBound = 1000;
}
else
cout <<" Invalid Game Level:";
cout << endl;
//generate a random number
srand(static_cast<int>(time(0)));
randomNumber = 1 + rand() % ((upperBound) - 1 + 1);
//get first number guess from user
cin >> numberGuess;
startTime = static_cast<int>(time(0));
while ( numberGuess != randomNumber)
{
if( numberGuess > randomNumber)
{
cout <<"Sorry, guess lower:";
cin >> numberGuess;
}
else if ( numberGuess < randomNumber)
{
cout <<"Sorry, guess higher:";
cin >> numberGuess;
}
attempts+=1;
}
cout << endl <<" Yes, the number is " << randomNumber <<"." << endl;
endTime= static_cast<int>(time(0));
totalSecond = endTime- startTime;
cout << endl <<" This is how many attempts you took:" << attempts << endl;
cout << endl <<" This took :" << totalSecond <<" seconds"<< endl;
cout <<" Do you want to play again? (Y/N)";
cin >> again;
cout << endl << endl;
}
system("pause");
return 0;
}
My attempts are adding all up together on each game why is this?? Like if I play one game it displays I took 5 attempts and when I play again say I took 3 attempts it will say I took 8 attempts why??Then it go on and on??