### While loop is not ending

Hi, I'm working on an assignment and we have to simulate the game of craps. When i go run my program it gets stuck and just keeps outputting and will not end.
Also, the purpose of this assignment is to have it functionally decomposed. That is why i have only one function in main.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257`` ``````#include #include #include #include using namespace std; void CrapsGame(); //1// // Will declare all variables and excute the game until donePlaying is true. void StartGame(int&, int&); //2// // This function will control the input and ask the player to start - // the game and will get the bank balance and wager from the player. int CheckWager(int, int&); //3// // This function will take the wager and the bankBalance and make sure they are valid. void GenerateRandomChatter(); //4// // Generates a random number and then outputs the phrase corresponding to that number. int GenerateRandomRoll(); //5// // Generates two random numbers for rolling dice and then adds them to get the point. char CheckDice(int, char&); //6// // Checks the point to see if the player won, lost, or has to keep rolling. bool CheckBalance(int, bool&); //7// // Checks to see if the new bank balance is 0 and outputs message if it is. char CheckRoll(int, int, char&); //8// // Checks the players new roll to see if they won, lost, or has to keep rolling. int NewBalance(int, int, char&); //9// // This function will take the original bankBalance and add or // subtract the wager - chosen based on whether or not the player won. void AskToPlayAgain(bool); //10// // Asks the player to enter y or n to keep playing or to quit playing. int main () { CrapsGame(); // Function called and will only fall out when donePlaying is true. return 0; } void CrapsGame() //1// { int bankBalance = 0; int wager = 0; int point = 0; string chatter; bool donePlaying = false; char win = ' '; int newRoll; int randomInt; srand(time(NULL)); cout << "Welcome to the Craps Simulation" << endl; while(!donePlaying) // Will repeat game until donePlaying is true. { StartGame(bankBalance, wager); // This function will control the input and ask the player to start - // the game and will get the bank balance and wager from the player. CheckWager(bankBalance, wager); // This function will take the wager and the bankBalance and make sure they are valid. GenerateRandomChatter(); // Generates a random number and then outputs the phrase corresponding to that number. point = GenerateRandomRoll(); // Generates two random numbers for rolling dice and then adds them to get the point. cout << point << endl; win = CheckDice(point, win); // Checks the point to see if the player won, lost, or has to keep rolling. int rollCount = 0; // Declares rollCount as an int and starts at 0. while(win != 'Y' || win != 'N') // Repeats steps until the user won or lost. { ++rollCount; cout << "Roll " << rollCount << " -> "; newRoll = GenerateRandomRoll(); // Generates two random numbers for rolling dice and then adds them to get the point. cout << newRoll << endl; win = CheckRoll(point, newRoll, win); // Checks the players new roll to see if they won, lost, or has to keep rolling. } int newBalance = NewBalance(bankBalance, wager, win); // This function will take the original bankBalance and add or subtract the wager - // chosen based on whether or not the player won. CheckBalance(newBalance, donePlaying); // Checks to see if the new bank balance is 0 and outputs a message and ends the game if it is. AskToPlayAgain(donePlaying); // Asks the player to enter y or n to keep playing or to quit playing. } } void StartGame(int& bankBalance, int& wager) //2// { cout << "Press return to roll your dice..."; cin.get(); cout << endl; cout << endl; cout << endl; cout << "Please enter your Bank Balance -> "; cin >> bankBalance; cout << endl; cout << "Enter your wager -> "; cin >> wager; cout << endl; } int CheckWager(int bankBalance, int& wager) //3// { while(wager > bankBalance || wager == 0) { cout << "Invalid wager - must be between 0 and " << bankBalance << endl; cout << "Please reenter -> "; cin >> wager; } return wager; } void GenerateRandomChatter() //4// { int randomInt = (rand()% 5 + 1); if(randomInt == 1) { cout << "Oh, you're going for broke, huh?" << endl; } else if(randomInt == 2) { cout << "Aw cmon, take a chance!" << endl; } else if(randomInt == 3) { cout << "You are up big. Now's the time to cash your chips!" << endl; } else if(randomInt == 4) { cout << "Aren't you a big spender!" << endl; } else if(randomInt == 5) { cout << "The dice may get you this time!" << endl; } } int GenerateRandomRoll() //5// { int die1 = (rand()% 6 + 1); int die2 = (rand()% 6 + 1); cout << die1 << " + " << die2 << " -> "; int point = die1 + die2; return point; } char CheckDice(int point, char& win) //6// { if(point == 7 || point == 11) { cout << "You win!" << endl; win = 'Y'; } else if(point == 2 || point == 3 || point == 12) { cout << "You lose!" << endl; win = 'N'; } else { cout << "Your point is -> " << point << endl; } return win; } bool CheckBalance(int newBalance, bool& donePlaying) //7// { if(newBalance == 0) { cout << "Sorry, You busted!" << endl; donePlaying = true; } else { donePlaying = false; } return donePlaying; } char CheckRoll(int point, int newRoll, char& win) //8// { if(newRoll == 7) { cout << "You lose!" << endl; win = 'N'; } else if(newRoll == point) { cout << "You win!" << endl; win = 'Y'; } return win; } int NewBalance(int bankBalance, int wager, char& win) //9// { int newBalance; if(win == 'Y') newBalance = bankBalance + wager; else if(win == 'N') newBalance = bankBalance - wager; return newBalance; } void AskToPlayAgain(bool donePlaying) //10// { char ans; cout << "Would you like to play again (y or n)? "; cin >> ans; while(ans != 'y' and ans != 'n') { cout << "Invalid responce -- please reenter "; cin >> ans; } if(ans == 'n') { donePlaying = true; } else if(ans == 'y') { donePlaying = false; } } ``````
Pass donePlaying by reference to AskToPlayAgain so that it saves your result.

 ``12345678`` ``````void AskToPlayAgain(bool&); // ... void AskToPlayAgain(bool& donePlaying) //10// { // ... }``````
Last edited on
You also have a couple of problems with your win variable.

1) Line 79: Your if logic is flawed. The expression will always be true. if win is ' ', win != 'Y'will be true. If win is 'Y', win != 'N' will be true. If win is 'N', win != 'Y' will be true.

2) When you play again, you never reset win back to ' '.

Hello Morris7498,

Ganado and AbstractionAnon make very good points.

What caught my attention is the fact that the function "AskToPlayAgain" is a void function that returns nothing and receives a variable, "donePlaying", by value. This means that any change to "donePlaying" is lost when the function ends. So the calling function stills sees "donePlaying" with its original value.

"AskToPlayAgain" eithers needs to return a value or, as Ganado said, "donePlaying" needs to be passed by reference.

My other thought about "AskToPlayAgain" is that it deals with only lower case letters, so if someone enters an upper case "Y" or "N" the result will not be what you want.

After line 239 you might consider `ans = toupper(ans);` or "tolower" whichever you want to use and the rest of the code would work better.

In the "CrapsGame" function until you change the value of "donePlaying" you will always have an endless loop.

Hope that helps,

Andy
Ganado, AbstractionAnon, and Handy Andy, thank you guys for all of the critiques. This helped so much and now it works.

Thanks again,
Morris7498