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
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.