Input Validation in C++

SpS 0 Tallied Votes 834 Views Share

Code will check whether the number entered is integer or not. ignore extracts characters from the input sequence and discards them. The extraction ends when max characters have been extracted and discarded or when the character delim(\n) is found, whichever comes first.
In the latter case, the delim character itself is also extracted. numeric_limits<int>::max() - Returns the maximum finite value for integer. gcount - returns the number of characters extracted by the last unformatted input operation performed on the object.

#include <iostream>
#include <limits>

using namespace std;

int main() {
  int number = 0;
  cout << "Enter an integer: ";
  cin >> number;

  cin.ignore(numeric_limits<int>::max(), '\n');

  if (!cin || cin.gcount() != 1)
    cout << "Not a numeric value.";
  else
    cout << "Your entered number: " << number;
  return 0;
}
munyu 0 Newbie Poster

nice trick

JLChafardet 0 Newbie Poster

may be a nice trick, but doesn't works, i have been trying to validate input for numbers for a long time, and this isn't working on my program.

we are a 2nd semester IT Computer Degree class, we haven't seen pointers nor regex so that's not an option as we cannot use what we haven't been taught in classes.

I have the following switch/case on my code.

case 'p':
    case 'P': Profesor:
              gotoxy(5,23);
              blanco
              cout << "Ingrese la cedula del profesor: ";
              rojo
              //cin.ignore(1,'\n');
              cin >> a;
              cin.ignore(numeric_limits<int>::max(), '\n');

              if (!cin || cin.gcount() != 1)
              {
              cout << "No es un valor numerico.";
              }
              blanco
              gotoxy(5,25);
              cout << "Ingresar el nombre y apellido del profesor: ";
              rojo
              getline(cin, b);
              blanco
         if(profesor[0] != "")
         {
              gotoxy(5,27);
              verde
              printf("%c", 175);
              rojo
              cout << " Ya ha definido un profesor.";
              blanco
         }
         else if(a == "" || b == "")
         {
              gotoxy(5,27);
              verde
              printf("%c", 175);
              rojo
              cout << " No introdujo Nombre o Cedula.";
              blanco
         }
         else
         {
              profesor[0]=a;
              profesor[1]=b;     
         }
    break;

i need to validate, if a is not numeric, should print an error (which it doesn't do.)

the rest of the program runs pretty well, and is amazingly well formed, its about 600 lines of code atm, and it will grow.

the next part, which i will have to do between today and tomorrow, also contains the need for this numeric validation, as it will be the grades array, where I'm supposed to store 3 grades per student, and the grades needs to be between 0 and 20, only numerical values should be accepted.

the parts of the 0 to 20 are easy enough, but i cant get the numeric validation thing to work, all help is appreciated.

JLChafardet 0 Newbie Poster

I managed to solve my problem by adding
cin.clear(); on the numerical validation, that managed to stop the infinite loop.

thanks anyway for this, it was really really useful.

// Caso profesor.
    case 'p':
    case 'P': Profesor:
         if(profesor[0] != "")
              {
                  gotoxy(5,27);
                  verde
                  printf("%c", 175);
                  rojo
                  cout << " Ya ha definido un profesor.";
                  blanco
              }
              else
              {
                  gotoxy(5,23);
                  blanco
                  cout << "Ingrese la cedula del profesor: ";
                  rojo
                  cin.ignore(1,'\n');
                  cin >> ci;
                  cin.ignore(numeric_limits<streamsize>::max(), '\n');

              if (!cin || cin.gcount() != 1)
              {
                  gotoxy(5,27);
                  verde
                  printf("%c", 175);
                  rojo
                  cout << " No es un valor numerico." << flush;
                  cin.clear();
                  break;
                  blanco
              }
              else
              {
                  blanco
                  gotoxy(5,25);
                  cout << "Ingresar el nombre y apellido del profesor: ";
                  rojo
                  getline(cin, b);
                  blanco
              if(b == "")
              {
                  gotoxy(5,27);
                  verde
                  printf("%c", 175);
                  rojo
                  cout << " No introdujo Nombre.";
                  blanco
              }
              else
              {
                  stringstream out;
                  out << ci;
                  a = out.str();
                  profesor[0]= a;
                  profesor[1]= b;     
              }
         }
         }
    break;
mrnutty 761 Senior Poster
int i = 0;
 

while(!cin >> i)
{
   cout<<"\nNot valid Try again : "<<endl;
   cin.clear();
  while(cin.get() != '\n')
   ;
}

cout<<"Valid\n";
VikyTushar 0 Newbie Poster
long long  int number = 0;
cout << "Enter an integer: ";
cin >> number;
cin.ignore(numeric_limits<int>::max(), '\n');
if (!cin || cin.gcount() != 1)
    cout << "Not a numeric value.";
else
    cout << "Your entered number: " << number;
return 0;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.