Hello everyone, this is my first post. I'm having trouble doing while loops. I've lost a LOT of points in my programming class because of this and I do not want to fail.
If you guess the right number and try to input anything the program just repeats "Do you want to play again? Y or N"
Also if you enter any character before guessing the right number the program forever repeats "Guess a number between 1 and 10: "
#include <iostream>
#include <ctime>
usingnamespace std;
int main()
{
srand(static_cast<unsignedint>(time(0)));
int count = 1;
char ans;
while (toupper(ans = 'Y'))
{
int num = rand() % 10 + 1;
int guess;
cout << "Guess a number between 1 and 10: " << endl;
cin >> guess;
while (guess != num)
{
count++;
cout << "Guess a number between 1 and 10: " << endl;
cin >> guess;
}
while (guess = num)
{
cout << "It took " << count << " Guesses to guess correctly" << endl;
cout << endl << "Do you want to play again? Y or N" << endl;
cin >> ans;
}
}
return 0;
}