RE: Craps Game 11-04-2013, 01:27 AM
#11
(11-04-2013, 12:54 AM)chmod Wrote:(11-04-2013, 12:30 AM)ArkPhaze Wrote: I guess I'll post this here since you didn't get what I was hinting at. I just wanted to see if you could find it on your own.
Code:int bank = 1000; int bet; scanf("%d", &bet); while (bet > bank || bet == 0) {} std::cout << "Your bet: " << bet << std::endl;
Result:
Code:-234 Your bet: -234
This is critical here, and something most often forgotten about. 'int' is a signed datatype; it extends into integer values below 0 as well.
Your completely right and it's something I overlooked, fixed now. Thanks again for your input I appreciate it, while we are on the subject the program doesn't really parse things efficiently anyway and entering anything but an integer breaks it so I'm going to play around with the code tomorrow and try to fix this.
And this is why experience C programmers try to avoid scanf(), along with the fact that it leaves junk in the buffer from user input. The way around this is to use fgets() typically (do not use gets()), and parse the string manually for correct input, and parse to the type you need it to be. For that there are functions like atoi() from the <stdlib.h> header which return an integer from a c-string. If you felt like re-inventing the wheel for learning purposes, try to create your own function to do this. (*It is NOT too difficult, if needed, I could show you.)
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)

