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;
     
}

Fixed it.
Change your checkuserguess to this

bool checkUserGuess(int userGuess,int randomNumber) {
if(userGuess == randomNumber){
return true;
}
else{
return false;
}
}

Also you had forgot to initialize attempts as 0
like so.

int attempts=0;

//K0ns3rv

Thats great,thank you!

Can I ask why the two {} made the diffrence in my function,was it because it was returning true and false back into the main?

They didn't make a diffrent i just added them cuz when i pasted the code there was an extra line of space between every line and i prefer if statments that have curly brackets regardless.

EDIT: remeber to mark the thread as solved when you have no further questions.
//K0ns3rv

Cool..well it seemed to do the trick anyway,thanks again.

EDIT: It was down to not having my attempts variable initialised,which goes against one of the first things thought when learning programming!INITIALISE YOUR VARIABLES!

A few other things you should be doing && to represent AND in C++ not &

//What you did
else if(answer==false & userGuess>randomNumber){
cout << "You guessed incorrectly,too high!" << endl <<endl;
}

//What it should be
else if(answer==false && userGuess<randomNumber){
cout << "You guessed incorrectly,too high!" << endl <<endl;
}

Will compile either way but with just one & gives an warning during debug.
Also you only have

main{

//TODO
}

You should be doing

int main{

//TODO

return 0;
}

One last thing your brace style is all over the place, as I am assuming this is for school you might want to pick one style and stick with as it will drive teachers insane if you don't.

Thanks for the tips,can I ask what the importance of return 0; is with int main,we're not thought that in college but ive seen it on plenty of code online.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.