while loop trouble

i want to play this game my teacher has helped us create, Devil's Dice. my problem isn't getting the game to work, its getting the game to replay itself at the end when the prompt asks me if i would like to play again...if your response is no, it closes just fine, but if the response is yes, it will keep asking you and repeating the score until you say no...please help me on this..

//(game code that doesn't matter really
do{
while (!gameOver)
{
if (myTurn)
{
printBoard(die, myScore, devilScore, myScore + advanceScore, myTurn);
cout << "Pass[p] or Roll[r]?!.\n";
cin >> input;
if (input == 'r')
{

//(game code that doesn't matter really.....

cout << "\n";
cout << " win/loss record: " << wins << "/" << losses << " (";
cout << 100 * wins / (wins + losses) << "%)\n";
cout << "\n";
cout << "play again? [y/n]:";
cin >> action;
}while (action == 'y');
// i'm aware that the game should start over, but unfortunately don't
//understand how to add another do loop and have the game start over, sorry :)
return 0;
}
You're probably suffering from an istream error. Probably because the game is reading a single character (you haven't shown the types and it "really does matter"), and you're typing a string.

Whatever the cause, after using "cin >>", check for errors and reset the istream state, also flush the input buffer:

1
2
3
4
5
6
7
if (!std::cin)
{
    std::cin.clear();
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}

action = std::cin >> action;


remember to match your input to the proper type
you may like to reset gameOver to false before you start again.

i would suggest here....
1
2
3
do{
gameOver = false;
while (!gameOver)



EDIT: there are also other variables you should probably set when a game is ready to begin, such as scores etc.
Last edited on
Topic archived. No new replies allowed.