Validate input values

For input value from the keyboard, I usually use cin. So what is “cin”?

To answer this question, I look its definition up in C++ reference and books. Cin is a object of class istream.

Standard input stream
Object of class istream that represents the standard input stream oriented to narrow characters (of type char). It corresponds to the C stream stdin.The standard input stream is a source of characters determined by the environment. It is generally assumed to be input from an external source, such as the keyboard or a file.As an object of class istream, characters can be retrieved either as formatted data using the extraction operator (operator>>) or as unformatted data, using member functions such as read.The object is declared in header <iostream> with external linkage and static duration: it lasts the entire duration of the program.

It little intiminates us. Let’s try something easier and more interesting. Simple syntax of cin is:

cin>>variable;

For types as string, where input value can be seperated by space and only be finished by a newline character (enter of \n). In this case, we use getline as following:

getline(cin,variable);

However this syntax is not applicable for numerical inputs.

Another thing is after cin and before getline we should imply cin.ignore() to avoid the fact that cin>> leaves the newline character. That will suddenly impact the following getline. To be clear, see the following syntax:

cin>>variable;cin.ignore();

getline(cin,another_variable);

Now, the thing I wanna discuss is verification of input values via cin. For example, if I have an integer variable as var1, then I need get it from the keyboard. The issue is that how can I control its value?

#include<iostream>
using namespace std;
int main(){
    int var1;
    cout<<"The value of var1:\t";     cin>>var1;
    cout<<"The input value is:\t"<<var1<<endl;
}

If I enter some alphabets, let’s say “er”, the output of cout will be 0. It means the value of var1 is 0. I would like to control the input process, it will require users enter a numerical value not alphabetical. To validate input value, I use istringstream class and getline function as following:

#include<iostream>
#include<sstream>
using namespace std;
int main(){
    int var1;
    bool flag;
    cout<<"The value of var1:\t";     do{         string temp;         getline(cin,temp);         istringstream sin(temp);         sin>>var1;
        flag=sin;
        if (!flag){cout<<"Enter a valid input:\t";}
    }while(!flag);
    cout<<"The input value is:\t"<<var1<<endl;
}

Using template function, I can write a function to check validate input value. The function is:

template <class T>
void validate(T &var1){
    bool flag;
    do{
        string temp;
        getline(cin,temp);
        istringstream sin(temp);
        sin>>var1;
        flag=sin;
        if (!flag){cout<<"Enter value of "<<typeid(var1).name()<<":\t";}
    }while(!flag);
}

The implimentation is:

#include<sstream>
#include<typeinfo>
using namespace std;
template <class T>
void validate(T &var1){
    bool flag;
    do{
        string temp;
        getline(cin,temp);
        istringstream sin(temp);
        sin>>var1;
        flag=sin;
        if (!flag){cout<<"Enter value of "<<typeid(var1).name()<<":\t";}
    }while(!flag);
}
int main(){
    double var1;
    bool flag;
    cout<<"The value of var1:\t";
    validate(var1);
    cout<<"The input value is:\t"<<var1<<endl;
}
Advertisements