Hi,
I am having a bit of a problem with this segment of code. The purpose of the function is to allow a user to make a wager between 1 and 5 (dollars, units, pesos, euros, whatever you want to call them). The wager cannot be greater than the amount of credits they have. So, if they have 2 credits, they canot make a wager of 5. So to sumarize.
- wager must be no less than 1 and no greater than 5
- wager may not be greater than number of credits left
The code I have below works fine for that. However, when the user enters a character or a string, the program jumps into an infinite loop. I want to ensure it alerts the user of the invalid input and asks them again for thier wager amount. I am trying to use the cin.good() function, but as you can see, I'm doing something wrong. scanf() would be much easier here, but I am not allowed to use C-style I/O for this assignment. Any help would be greatly appreciated.
Code:int getWager(int credits) { bool validWager=false; int wager; while(validWager == false) { cout<<"Place bet (1-5): "; cin>>wager; if(wager<=5 && wager<=credits && wager!=0 && cin.good() == true) validWager = true; else cout<<"Invalid Wager!"; cout<<endl; if(!cin.good()) {cin.clear(); cin.sync();} } return wager; }