What does exactly happen when you insert wrong type input?

Hello,
I've been wondering.
I'd like to understand how inputs are handled.

Let's say I want receive an integer.

we have

int age;

I want user to input his age, and the process this integer.

cin>>age;

But what if user isn't smart enough and enters something different, like character('a'), or string("aaa")?
Can I control the flow of the program to go back to the point of inputing the age, to prevent this type of error?

Now, since I don't know the answer to above(I'd like to), I have to find another way.

So I create
char preAge[10];
a nice string to let user input whatever he wants to. Than, I'd convert it to int with atoi.
Now, code would look like this
1
2
3
4
5
do{
cin.getline(preAge, 9);
}while( !atoi(preAge) )// atoi returns 0 if it's not an integer
age = atoi(preAge);
cout<<"You are "<<age<<" years old!";

Great. But yet there are even more possible problems:
1) If it's not an age, but some other data with which I'm using this method, than user can't insert 0, since program would think that it's a bug.
2)If user wants to use a string longer than 9 characters... Than what happens?

I think that I should use try..catch, but I don't know how.

I wanted to test it with this simple program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {

char test[4];
cout<<"I'm waiting"<<endl;
cin.getline(test, 4);
cout<<atoi(test);
cin.getline(test, 4);
cout<<test;
cin.get();

return 0;
}

Now, in this program, if I enter a string longer than 3 characters, than my program would show only 3 characters(as a number), and then return 0(without getting another input nor printing it in unchanged form). I guess that some exception is thrown, but again, I don't know which.

How should I try to solve these problems? Should I try to use string instead of integers and then convert them when I'm sure?
How should I create program to be free from problems when user enters too long string?
I'm really curious, nobody knows?
ios comes with some helpful methods to check to whether data is valid or not.

http://www.cplusplus.com/reference/iostream/iostream/

Scroll down to "Member functions inherited by ios"
1
2
int age;
cin >> age; 


If we input 'a', age will receive 0x61 or 0110 0001. So if you cout age again, you'll get 97 (http://www.asciitable.com/).

If we input 3.13235, it will ignore the decimal and store 3.

If we input "aaa", it will receive 'a', and will ignore the next two entries. I think those are flushed from the stream during the next cout operation, but it's possible that they will just be used in the next cin >> operation.

Last edited on
If you >> a non-number when an istream (cin/ifstream/etc) expects a number, you break the input object. Since >> returns the state of the object you can do something like this:
while (!(cin >> number))

In that loop, you have to fix cin, cin.clear(), and then clear the buffer (I don't think the faulting character was extracted), cin.ignore(80, '\n'). Then output an error message.

To stick with atoi(), as you know, this function returns 0 on failure. As a check, I think:
if (strlen(preAge) > 1 && preAge[0] != '0')
would be enough to know that the user didn't really type 0 (or 000000000).

I do think the atoi version is more valuable to learn. If you make a text box in a GUI, anything in there will be a string. If you need a number, you need atoi. If you have complicated input (from an .csv or something), it seems better to extract an entire line from the file and then figure out what is a number and what isn't.
Last edited on
Thanks a lot! That helped.:)
In that loop, you have to fix cin, cin.clear()

i've tried this but it doesn't work...

and then clear the buffer (I don't think the faulting character was extracted), cin.ignore(80, '\n')

i haven't try this one...
^^ Something like this
1
2
3
4
5
6
7
8
9
10
11
12
double getNum(void)
{
  double num;
  while (!(cin >> num))
  {
    cin.clear();
    cin.ignore(80, '\n');
    cout << "Numbers only please. Try again: ";
  }
  cin.ignore(80, '\n');
  return num;
}


And if you do get into GUI, I know Win32 edit windows have the option to only accept numerical values. I guess this will ensure a valid atio.
Last edited on
I do think the atoi version is more valuable to learn [...] there will be a string. If you need a number, you need atoi.

I disagree: atoi() is practically useless because it returns zero on error: you have to write extra code to reexamine the input by other means.

If you have a string and need a number, use strtol() (and similarly-named functions) from the C library or use operator>> or stoi() from C++, or lexical_cast from Boost.
Topic archived. No new replies allowed.