Thread: Check input content

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    57

    Check input content

    Hmmm...
    Lets say we have
    Code:
    int a;;
    cout << "Please input a number";
    cin >> a;
    If the user input a letter, the problem will crash, of course.
    How do i check if it will happen? So i can do something like: "Please, input the number again".

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Always read user input as a string. If it is supposed to be numeric, attempt to convert it. If the conversion fails, reprompt.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    I dont know how to check if an string is composed only by numbers...
    Are you sure that theres not another way? =~~

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I dont know how to check if an string is composed only by numbers...
    http://www.parashift.com/c++-faq-lit....html#faq-39.2

    >Are you sure that theres not another way? =~~
    There are plenty. I've just tried to shorten your process of learning several dozen things that have issues with them and just get you to the bottom line.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You don't need to. You simply check if the stream is valid.

    Code:
    int a;
    cout << "Please input a number";
    if(cin >> a) {
       // input was successful. a is holding the value
    else {
       // anything other than an int was introduced
    }
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Ok, Thank you both =]

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If you want to catch things like 123a as a non-number, you may want to review what I mentioned earlier.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The stringstreams have the same issues (and very similar solutions) as just checking cin. I'd still just check cin and add the extra checks to it as necessary.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Yes, but string input makes cleanup easier IMO.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. Not working in linux...
    By mramazing in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2009, 02:18 AM
  3. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  4. check if user input matches a word
    By fakebloo in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 07:12 PM
  5. h/w help
    By helpme in forum C Programming
    Replies: 20
    Last Post: 10-21-2003, 09:36 AM