Input validation C++

Hello!
My question is if I only want the user to input a int, how do I create a loop that will continuously ask the user to try again if they input the wrong data type or if they don't enter anything at all. Thanks.

ps: I'm not asking for the syntax of any kind of loop just want to know what to set the condition to in order to check that the input is valid.

Depends what's your definition of "valid input" and "invalid input".

If by valid you mean that the int value is a number, you can do a do-while loop that loops if the input value is not valid.
In C I would do:

do {
    printf("Insert number: ");
    scanf("%s", s);
} while (!isNumber(n));
int n = atoi(s);

bool isnumber(const char *s) {
   char* e = NULL;
   (void) strtol(s, &e, 0);
   return e != NULL && *e == (char) 0;
}

This one is a little cumbersome but I learned this from my professor. You are going to need to use the preprocessor directives #include<limits> and of course #include<iostream>. You can use using namespace std; on a new line if you are using the standard name space.

The process uses a boolean and a do while loop.

while inside the main:
of course instantiate your integer as
int x;
and your boolean as
bool nFail = false;

now that you have those instantiated, you can set up your do while loop
I set it up as
do {
cout << "enter and integer" << endl;
cin >> int x;
nFail = cin.fail(); //we set our bool to cin.fail() to see if it fits the parameters of the entered value
cin.clear(); //clears the input the user entered while not setting the value
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //forgive me as I am not entirely sure how this one works.
} while (nFail == truie);

1 Like

cool thanks Theonewhoisdrunk. I'm curious about the ignore function, going to look that guy up. Oh and btw the "::" what exactly is that ?

The :: operator is called the scope-resolution operator and does just that, it resolves scope. So, by prefixing a type-name with this, it tells your compiler to look in the global namespace for the type.

1 Like

What about if the user enters a double? Also how would you handle input validation for a string (only letters) using getline()?

Most of the code here is so... cumbersome....

In (more or less pure) C-like C++ :

#include <cstdio>
using namespace std;
int main()
{
    do {
       int temp = 0;
       if (scanf("%d", &temp) != 1) 
         printf("Try again.\n");
      else {
          // do something with number.
         break; // eventually
       }
   } while(true);   
   return 0;
}

Or correct C++ way:

#include <iostream>
using namespace std;
int main()
{
    int temp = 0;
   while (true) {
       if (!(cin >> temp)) {
         cout << "Try again." << endl; 
      } else {          
         // do something with number.
         break; // eventually
     }
   } 
   return 0;
}

Or even more paranoic way:

#include <iostream>
#include <limits>
using namespace std;
int main()
{
    int temp = 0;
   while (true) {
       if (!(cin >> temp)) {
         cout << "Try again." << endl; 
         cin.clear();
         cin.ignore(numeric_limits<streamsize>::max(), ' '); // skipping
      } else {          
         // do something with number.
         break; // eventually
     }
   } 
   cout << temp << endl;
   return 0;
}

But the best answer is not to handle data that are corrupted in any way and just quit after detecting the bad input.

2 Likes

or even like this...

#include <iostream>
using namespace std;
int main()
{
    int temp = 0;
    while (!(cin >> temp)) cout << "Try again." << endl; 
    // do something with number.
    return 0;
}
2 Likes