INFINITE LOOP PROBLEM

Im making a casino program for a project. Im making the craps game and right now Im just getting the basics out the way like the menu and the betting down. So everything works fine, but when I was testing the program so far, and it asks to place your bet, if you put in a decimal ... it goes into a infinite loop. Im new to c++ so if you could help me out on trying to figure whats causing it Id appreciate it ! Thanks

(My first Post btw ) lol .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

void crapsGame()
{
    int bet;
    
    clearscreen();
    
    cout << "--------------- C R A P S ---------------"<< endl;
    cout << "\n\n" << endl;
    do {
        cout << "Your Money: " << money << endl;
        cout << "Place Your Bet: ";
        cin >> bet;
        
        if(bet < 1)
        {
            cout << "\nYou placed lower than the minimum bet.\n" << endl;
            
        }
        else if(bet > money)
        {
            cout << "\nYou betted more money than you have.\n" << endl;
        }
    } while (bet < 1 || bet > money);
      
}
    
    
Not sure if this helps but I would avoid using a decimal if you have a variable of type int declared. Since your dealing with money it would be better to use double or float. I'm a beginner as well but I wish you the best of luck.
Zed, yeah thats what I want to avoid, but my teacher makes us test for everything that might break our code because we dont know what the user might try to input .

So if the user inputs a decimal on accident or for some reason then it goes into a infinite loop and idk what to do to make that not happen.

Thanks for the reply though ! If you got anything else I'd appreciate it ahah.
When you type a decimal value, the >> to int extracts only the number before the decimal point. The next iteration (or next attempt to >>) will see the decimal point as first character in the stream. Decimal point is not part of an integer, so the input fails and the stream has an error flag set. When stream has an error flag, it will automatically fail all input operations. The 'bet' will hold the last successful input, which obviously was valid for your loop, so the loop keeps rolling.

One can (and should) check the state of the stream and clear() error flags, but you have to check what was the error in order to proceed appropriately.
Consider, which values an int variable can hold.
Besides: where is the definition of money? Why is it global?
ok to fix this basically only option is instead of using int for the betting system I would have to use a float or double right ?

And money is global because I have to make a casino program with three different games in it , and the money has to transfer between all three games.

Thanks tho guys really helped ! I actually just finished the craps game last night if you guys wanna try it out ill post it up .
Topic archived. No new replies allowed.