Search code examples
loopswhile-looppromptcin

using while (!(cin ... and getting blinking prompt


The problem I am having is when I'm going through the program it stops at "you bet _". When I take out the while (!cin) line it works right. I'm not sure why this is being executed and making the blinking prompt. What am I missing? Thanks.

It's this block that is being executed when it shouldn't be when I give it a good number.

while (!(cin >> bet) || bet < 0) 
{
        cout << "Bad input - try again: ";
        cin.clear();
        cin.ignore(INT_MAX, '\n'); // NB: preferred method for flushing cin
    }

Here is the full program:

 Create a program to allow a user to play a modified “craps” games. The rules will be as follows:

 1) User starts with $50.00 as their total.
 2) Ask the user for their bet (maximum bet is their current total).
 3) Throw a pair of dice.
    a)  If the total value is 7 or 11, the user is an instant winner. The user has the amount bet added back into their total. Ask the user if they wish to play again and if so they start at step two above.
    b)  If the total value is 2, 3, or 12, the user is an instant loser. The user has the amount bet deducted from their total. Ask the user if they wish to play again and if so they start at step two above.
    c)  If the total is anything else, remember this total as the “point” and roll again.
    i)  If the new total is equal to the “point”, the user wins and the process is the same as winning in (a) above
    ii) If the new total is a 7, the user loses. And the process is the same as losing in (b) above.
    iii)    If the new total is anything else the user must roll again and again try to match the “point” of the first roll.


 */

// user input twice and closing out if user is out of money

#include <iostream>
#include "Header.h"
#include <fstream>


using namespace std;

int main()
{
    int money = 50;
    int bet;
    bool hasMoney = true;
    bool wantsToPlay = true;
    int roll;
    char yesOrNo;
    bool secondLoop = true;
    InitDice ();


    // Loop (user has money && they want to play)
    while (hasMoney && wantsToPlay) {
        // Get Bet
        cout << "How much would you like to bet?" << endl;
        cout << "$";
        cin >> bet;
        cout << "You bet " << bet << endl;
        while (!(cin >> bet) || bet < 0) // <<< note use of "short circuit" logical operation here
        {
            cout << "Bad input - try again: ";
            cin.clear();
            cin.ignore(INT_MAX, '\n'); // NB: preferred method for flushing cin
        }

        if (money > 0) {
            cout << "You ran out of money so we are done here." << endl;
            hasMoney = false;

        }


        while (bet > money) {
            cout << "Please bet below your current balance: $" << money << endl;
            cout << "$";
            cin >> bet;
        }
                // Throw Dice
        roll = ThrowDice();
        cout << "You rolled a " << roll << endl;
        // if (Total is 7 or 11)
        if ((roll == 7) || (roll == 11)) {
            //  Win
            cout << "You win!!!" << endl;
             //  Add Bet to their money
            money = bet + money;
            cout << "Your current balance is now: $" << money << endl;
            //  Continue loop

        //if (Total is  2 or 3 or 12)
        } else if (((roll == 2) || (roll == 3) || (roll == 12))) {
            cout << "You lose!" << endl;
            money = money - bet;
            cout << "Your current balance is now: $" << money << endl;

        //if (Total is something else)
        } else {
            // Point is set to Total (money)
            money = roll;
            cout << "Your total has now been set to your roll which was " << money << endl;
            // second loop
                            //Throw Dice
            do {  // cant be a DO while loop I think
                cout << "Throwing the dice again.";
                roll = ThrowDice();
                cout << " You got a " << roll << "." << endl;
                //if (Total is Point)
                if (roll == money) {
                    cout << "You win!!!" << endl;
                    //Win
                    //add bet to their money
                    money = bet + money;
                    //Back to the outside loop (asking if they want to play again)
                    cout << "Do you want to play again? (y/n): " << endl;
                    cin >> yesOrNo;
                    if ((yesOrNo == 'y') || (yesOrNo == 'Y')) {
                        cout << "They do want to play again :D" << endl;
                        wantsToPlay = true;
                        break;
                    } else {
                        cout << "Okay, thanks for playing" << endl;
                        wantsToPlay = false;
                        exit(0);
                    }


                //if (Total is 7)
                } else if (money == 7) {
                    //Lose
                    // Subtract their bet
                    cout << "You lose. i need to get to outside loop ehre" << endl;
                    money = money - bet;
                    break;
                    // Back to the outside loop

                //if (Toal is something else)
                } else {
                    cout << "Roll again? (y/n): " << endl;
                    char rollAgain;
                    cin >> rollAgain;
                    if (rollAgain == 'y' || rollAgain == 'Y') {
                    } else if (rollAgain == 'n' || rollAgain == 'N') {
                        cout << "Well, you are walking away with $" << money << ". Thanks for playing!" << endl;
                        exit(0);
                    }

                }



            } while (secondLoop);
            }

                     }

}

Don't think it's relevant but here is the functions.cpp file

#include <stdlib.h>  // for random number functions
#include <time.h>
#include <ctype.h>
#include <string>



#include "Header.h"

using namespace std;


void InitDice ()
{
    srand (time (0));
}

int ThrowDice ()
{
    return ThrowDie () + ThrowDie ();
}

int ThrowDie ()
{
    int     Value;

    Value = rand ();   // rand gives back a random number from 0 thru 32767 (known as RAND_MAX)
    Value = (Value % 6) + 1;
    return Value;
}

Solution

  • The answer is pretty easy. You messed up with first cin call. Instead your code should look like:

    cout << "How much would you like to bet?" << endl;
    cout << "$";
    while (!(cin >> bet) || bet < 0) // <<< note use of "short circuit" logical operation here
    {
        cout << "Bad input - try again: ";
        cin.clear();
        cin.ignore(INT_MAX, '\n'); // NB: preferred method for flushing cin
    }
    cout << "You bet " << bet << endl;
    

    With your previous version you always asked user twice for input. First with the single line cin call and then in the while loop. That's why it got stuck. Note that code in the logical expression of while loop will be always invoked at least once.