Hi ,im having a problem with my do while loop,basically I want it to loop so long as the users attempt is less than 10.
However,it just doesnt want to seem to let me do this.Ive tried putting the "attempts++" several places through the loop as i thought this might be the problem but its not working.Probably a simple problem but just cant spot it.
Anyway,heres the code:
// Game that generates a random number between 1-100
// user must try to the guess number.
#include <iostream>
#include <ctime> // needed for random()
using namespace std;
int generateRandomNum();
// purpose : to generate a random number for user to try guess
// returns: randomly generated number between 1 - 100
bool checkUserGuess(int userGuess,int randomNumber);
// purpose: check if user guess matches randomNumber
// parameters: the users guess, and the randomNumber
// returns: a true or false value
main() {
int randomNumber;
int userGuess;
bool answer;
int attempts;
cout << "Try to guess the secret number between 1-100!" << endl;
// call function
randomNumber = generateRandomNum();
// random number is now generated
// time for user to guess number
do
{
cout << "Enter your guess.." << endl << endl ;
cin >> userGuess;
attempts++;
// call function to check user guess
answer = checkUserGuess(userGuess,randomNumber);
// tell user if guess was right or wrong
if(answer==true){
cout << "Correct,you win!" << endl << endl;
break;
}
else if(answer==false & userGuess>randomNumber){
cout << "You guessed incorrectly,too high!" << endl <<endl;
}
else if(answer==false & userGuess<randomNumber){
cout << "You guessed incorrectly,too low!" << endl << endl;
}
}while(attempts<10);
}
// Generates random number
int generateRandomNum() {
int randomNumber;
// generates random number between 1-100
srand(static_cast<unsigned int>(time (0)));
randomNumber = rand()%100 + 1;
return randomNumber;
}
// checks user guess
bool checkUserGuess(int userGuess,int randomNumber) {
if(userGuess == randomNumber)
return true;
else
return false;
}