Thread: Validating valid input?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    16

    Validating valid input?

    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;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,119
    Visit this link about istream::sync()

    EDIT: To be sure, use "cin.ignore()" instead
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    Quote Originally Posted by GReaper View Post
    Visit this link about istream::sync()

    EDIT: To be sure, use "cin.ignore()" instead
    great, thanks a lot! It works much better now with ignore() instead.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    "Validating valid input?"
    a.k.a "rubber stamping"?

    Doesn't one tend to validate "unvalidated" input?...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with validating an input.
    By morrjame in forum C Programming
    Replies: 9
    Last Post: 12-20-2010, 09:55 PM
  2. validating memory - minimum address of valid location
    By m37h0d in forum C++ Programming
    Replies: 12
    Last Post: 09-05-2008, 11:50 PM
  3. validating input
    By c-rookie in forum C Programming
    Replies: 15
    Last Post: 08-23-2003, 08:55 PM
  4. Validating an input
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-23-2003, 12:25 AM
  5. Validating input
    By Barjor in forum C# Programming
    Replies: 7
    Last Post: 02-27-2003, 09:42 PM
Website Security Test