Infinite loop

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.

Heres the code:

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>

using namespace std;

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

cout << "Play again? (y/n): ";
cin >> again;
}while(again != 'n');



//Common ending
cout << endl <<"(press any key to exit)" << endl;
getch();
return 0;
}

sorry about the bad formatting
Last edited on
Use [code] tags please

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.

Try changing 'again' to be a char.
Thank you
Topic archived. No new replies allowed.