0

My program contains a simple input code:

int number;
std::cin >> number;

Of course, if user types 3.14, only "3" will be read. That's ok for the further code in general, but I want that kind of input to be marked as invalid and prompt user to try again with a proper integer.

Is there a way to check if anything was typed after the int?

Denys Triasunov
  • 539
  • 1
  • 4
  • 24
  • Did you google at all? You can test the return value of your reading operation, example: http://www.cplusplus.com/forum/beginner/21595/ – Antonio Feb 12 '15 at 16:08
  • `if (!(std::cin >> number) || !cin.eof())` but since we are talking about standard input, there isn't really an “end of input”. Also note that we've had [a *very similar* question](http://stackoverflow.com/q/28479888/1392132) just an hour ago. – 5gon12eder Feb 12 '15 at 16:09
  • 1
    The formatted input functions of C++ are pretty useless for any situation where you want rigorous input validation. You can do it, but it gets really ugly. I almost always end up extracting delimited strings (like lines with `getline`) and then parsing them myself. – Joseph Mansfield Feb 12 '15 at 16:21
  • You could also assume the number is a float or double, and then find the floor/ceiling of that number. If the floor/ceiling of it is not equivalent to the original, then it's not an int. – FCo Feb 12 '15 at 16:23
  • @5gon12eder, The `eof` check is redundant as `!(std::cin >> number)` will be true if it hit EOF. Anyway, input could be redirected, or the user could enter ctrl-D or whatever variant. – chris Feb 12 '15 at 16:23
  • @chris Note that both checks are negated. The conditional will evaluate to `false` unless there was a valid `int` input *and* this was all input there was. It is true that the user can hit *C*-d or the like but it would be awkward if a program *mandated* that just to read an integer. The wording about stdin and eof in my previous comment was unfortunate, though. – 5gon12eder Feb 12 '15 at 16:38
  • @5gon12eder, Right, I don't know why I treated that as not negated. – chris Feb 12 '15 at 16:43

1 Answers1

0

I would suggest that take the input as a string and then parse it. For example :

string s;
std::cin >> s;

Write some function to check the input. For example : '0'<=s[i]<='9'

You could even use regex.

Update: As 5gon12eder said: You have std::stoi function for the same. It will convert the string to int if it is valid integer. Otherwise it throws an exception invalid_argument.

Rishit Sanmukhani
  • 2,159
  • 16
  • 26
  • The standard library already has a function for this: [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) – 5gon12eder Feb 12 '15 at 16:11
  • Agreed, but you never know what user is going to type. stoi converts string to int. But the input may not be an integer. – Rishit Sanmukhani Feb 12 '15 at 16:13
  • 4
    In which case `std::stoi` and friends will throw `std::invalid_argument` which you can `catch` and act upon appropriately. (NB: `stoi` != `atoi`) – 5gon12eder Feb 12 '15 at 16:15