Im createing a number guessing "game" for my computer programming class. It works fine up until the end where it askes you if you want to play again. If you input anything it crates an infinite loop. I can't figure out why.
int number = 0;
int start = 0;
int stop = 1;
char ok = 'a';
int playerNumber = 0;
int again = 'a';
int main()
{
do
{
do
{
cout << "What number would you like to start with?" << endl;
cin >> start;
cout << "What number would you like to end with?" << endl;
cin >> stop;
cout << "You have chosen to guess a number between " << start << " and " << stop << " OK? (y/n)" << endl;
cin >> ok;
}while (ok != 'y');
srand(time(0));
number = (rand() % stop) + start;
do
{
cout << "Chose a number between " << start << " & " << stop << endl;
cin >> playerNumber;
(playerNumber == number)? cout << "You guessed correctly YOU WIN!!!" << endl: (playerNumber > number)? cout << "Too high try again" << endl: cout << "Too low try again" << endl;
}while(number != playerNumber);
cin >> again; <--- this formats the input to an integer because 'again' is declared as an integer. Since characters like 'n' are not an integer, they do not get formatted properly.