0

I've been searching (on the internet, in books, anywhere) for a solution to my problem for some time and I can't seem to find it. I think there are plenty of posts about this subject, I just haven't found the right one I think and I think I never will, so I'm going to ask it myself.

At a certain point in a program that I'm writing, I need to get user input, as a positive integer. When the user puts in something different than an integer (or a negative integer), the program will respond with "Invalid input.". When the user types in an correct integers, it will continue doing what it has to do (in this case it will 'cout' the input). This is all going well, but things go wrong when the user types in:

"1a" (a number combined with a character), it will only recognize "1";

"1 2" (number space number), it will only recognize "1";

"1,2" (number comma/dot number), it will only recognize "1";

I'd like to get the "Invalid input."-output in all cases.

I'll post a simplified version of my code to show you what I've got so far and to ask you guys if you could provide me with a solution for this problem.

Thanks in advance!

#include <iostream>

using namespace std;

int main()
{
    int input;

    cout << "Enter a number higher than 0: ";
    cin >> input;

    while (cin.fail() || input < 1)
    {
        cout << "Invalid input.\nPlease enter a (positive integer) number higher than 0: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin >> input;
    }
    cout << "Your input is: " << input << endl;
    return 0;
}
  • If you google with `cin whole line`, you might get some hints. It could be a duplicate question in Stackoverflow. – Keugyeol May 29 '14 at 22:13
  • possible duplicate of [Reading a full line of input](http://stackoverflow.com/questions/5882872/reading-a-full-line-of-input) – Keugyeol May 29 '14 at 22:16
  • Maybe not a duplicate: after reading the full line of input, the question is still how best to parse the line so as to determine whether it contains extraneous text (which is classified as an input error). – David K May 29 '14 at 22:19

1 Answers1

3

Read a line of input via getline from the <string> header.

Trim the line, i.e. remove initial and trailing whitespace.

Parse that line e.g. via istringstream (or as the standard library does, via strtol).

Check where the parsing stopped.

If the parsing succeeded and consumed the whole string, you have good input.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • It may be a bit stupid to ask, but can you explain to me how I should do that (in code)? My programming experience is limited and I'm not getting anywhere now. I 100% believe this is a useful answer, but I still don't know what my code is supposed to look like right now. Thanks. – Ben Groeneveld May 29 '14 at 23:02
  • @BenGroeneveld: There is a usage example of `getline` at its [documentation page](http://en.cppreference.com/w/cpp/string/basic_string/getline) over at cppreference.com. I believe most of the rest is also just a matter of delving into the documentation, alternatively googling. For the trimming you need to be able to check whether a given character is whitespace, which you can do via `isspace`. Remember to cast the argument from `char` to `unsigned char`, since giving it a negative value is Undefined Behavior in general. Hm, I think that's all, mostly. Good luck! ;-) – Cheers and hth. - Alf May 29 '14 at 23:18