Thread: Catching bad input

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    Catching bad input

    Is there a simple way of catching bad input for integers? Say if someone enters a character instead?

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    One way (how i do it) is to come up with something like this:
    Code:
    int main() {
    //make an int array
    //if you're reading INTEGER from cin(), you must give it a size first 
    //to avoid compiling errors
    int INTEGER[1000];
    //set up boolean variable for use in while loop, set it to false (0)
    bool isOK="0";
    while (isOK=="0") {
    cout<<"enter integer"<<endl;
    cin>>INTEGER;
    //now that you have it, test it for letters
    //cycle through entire alphabet based on this example
    if (INTEGER[0]=='a'||'b'||'c') {
    //since it got to here, there were letters in the variable so set
    //bool to false, which will make the loop go again and ask for 
    //INTEGER until it checks out
    isOK="0";
    }
    //repeat for INTEGER[1], etc., up to a reasonable number,
    //obviously you don't want to go to 1000 (p.s. 1000 was just 
    //an example size
    //now if you checked for all the parts of the array, time to allow
    //it to break out if there were no letters
    else{
    isOK="1";
    }
    //that will break the loop if everything was fine, repeat just as 
    //the last set of checks
    //P.S. don't forget to place the else right after the } in each 
    //if statement, otherwise it won't compile correctly
    }
    good luck, if it doesn't work....consult an expert

    [edit]changed my mind, added else statements to code to avoid too much typing, sorry i didn't think of it first, it's been a long day[/edit]
    Last edited by Waldo2k2; 05-28-2002 at 05:44 PM.
    PHP and XML
    Let's talk about SAX

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    The way i've been looking at is something similar to this:

    Code:
    float af;
    int a;
    
    cin >> af;
    a = (int) af;
    
    if (cin.fail() || (af-(float)a != 0))
    throw CWrongDataType();
    Was just wondering if there was a simpler way....

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    like i implied, im not an expert, im not even all the way through the book i bought ...anyhow, that looked good and would worka heck of a lot better than what i proposed, use that...
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Catching all input and sending it to another program
    By RedWind in forum C Programming
    Replies: 13
    Last Post: 09-15-2008, 12:51 PM
  2. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM
  3. checks for bad input
    By kocika73 in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:28 AM
  4. h/w help
    By helpme in forum C Programming
    Replies: 20
    Last Post: 10-21-2003, 09:36 AM
  5. input files in C
    By LWisne in forum C Programming
    Replies: 4
    Last Post: 09-30-2002, 06:24 AM