dimanche 26 juillet 2015

Random number generator for C++ popping up with an error?

I am having trouble with my number generator. Syntax wise, everything is working properly. I mainly wanted to use functions to see if they would work properly. When I run the program, a message pops up and says that my variable "guess" is not initialized. Can anyone give insight as to why this may be happening? Also note that even though I didn't include my libraries in the code below, they are present in the actual program itself.

using namespace std;

int game();
string playAgain();

int main(){

game();
playAgain();

return 0;
}

int game(){

int guess;
int guessesTaken = 0;
int number = rand() % 10 + 1;
int count = 0;

cout << "I am thinking of a number between 1 and 10, can you guess it? " << endl;

while (guessesTaken < count){
    cout << "Take a guess: " << endl;
    cin >> guess;
    if (guess > number)
        cout << "Too High. Try again!" << endl;
    if (guess < number)
        cout << "Too Low! Try again!" << endl;
    if (guess == number)
        break;

}count++;

if (guess == number)
    cout << "Congratulations!" << endl;
return 0;
}

string playAgain(){
string play;

cout << "Want to play again?: " << endl;
if (play == "y" || "Y")
    main();
else
    cout << "Thanks for playin" << endl;
return 0;

}




Aucun commentaire:

Enregistrer un commentaire