Like Tree2Likes
  • 2 Post By iMalc

Validating valid input?

This is a discussion on Validating valid input? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am having a bit of a problem with this segment of code. The purpose of the function is ...

  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
    1,620
    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,295
    "Validating valid input?"
    a.k.a "rubber stamping"?

    Doesn't one tend to validate "unvalidated" input?...
    laserlight and GReaper like this.
    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, 08:55 PM
  2. validating memory - minimum address of valid location
    By m37h0d in forum C++ Programming
    Replies: 12
    Last Post: 09-05-2008, 10:50 PM
  3. validating input
    By c-rookie in forum C Programming
    Replies: 15
    Last Post: 08-23-2003, 07:55 PM
  4. Validating an input
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-22-2003, 11:25 PM
  5. Validating input
    By Barjor in forum C# Programming
    Replies: 7
    Last Post: 02-27-2003, 08:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21