Trouble crearing wrong input completely

I recently shared this program to get input on whether it was efficient coding. I was informed that the system("PAUSE") and system("CLS") were bad ideas. I haven't changed that in the program yet. Currently the function getBet() is used to get a value to be bet against the next hand. If the user inputs multiple letters by accident a condition statement tells them they had Invalid Input and then the conditionally executed if statement should clear the cin object. Some reason it access one or both of the other if statements as well. How can I completely clear the cin object to make the program run more smoothly.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void getBet(const double total, double& bet)
{
     bool inputGood = false;
     
     do
     {
     cout << endl;
     cout << "Your Total = $" << total << endl;
     cout << "Bet        = $";
     inputGood = std::cin >> bet;
     
     if(!inputGood)
     {  
      std::cout << "\nERROR! Invalid Entry." << endl;
      cin.clear();
      system("PAUSE");
      system("CLS");
      showWinningsMenu();
      while( cin.get() != '\n' )cin.clear();
     }
     
     if(bet < 0)
     {
      cout << "No Negative Numbers. \n";
      cin.clear();
      system("PAUSE");
      system("CLS");
      showWinningsMenu();
      while( cin.get() != '\n' )cin.clear();
     }
     
     if(bet > total)
     {
     cout << "You dont have enough money to make that bet.\n";
     cin.clear();
     system("PAUSE");
     system("CLS");
     showWinningsMenu();
     while( cin.get() != '\n' )cin.clear();
     }
     }while(!inputGood || bet > total || bet < 0);
     
     while( cin.get() != '\n' )cin.clear();
}

cin.clear(); only clears the error flags; it doesn't read/discard any characters in the buffer.

For that, you can do something like
1
2
3
4
5
6
while (!(cin >> bet))
{
    cin.clear(); // Clear error flags
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Also #include <limits> for this
    // Print error message here...
}


What cin.ignore(numeric_limits<streamsize>::max(), '\n'); does is basically ignore (discard) as many characters from the input buffer as possible until it reaches a newline ('\n') character.

http://www.cplusplus.com/reference/istream/istream/ignore/

EDIT: Also, in your code, since you don't try to grab a new input if the user enters an invalid one until the next time the do-while loop loops around, if it so happens that inputGood is false and whatever junk value is in bet is negative or greater than total, then you're going to run right into those other if statements as well, which is probably the problem you're trying to describe.

Try replacing the if on lines 22 and 32 with else if and see if that's any better.
Last edited on
Thanks long double I will make those changes when I get back home and resend the code if I have anymore issues.
Topic archived. No new replies allowed.