Input Validation is an integer

I typed this and he asks the user to enter two integers, which will then become variables. From there, he will perform simple operations.

How to make the computer verify that the input is an integer or not? And if not, ask the user to enter an integer. For example: if someone enters "a" instead of 2, then he will tell them to enter the number again.

thanks

#include <iostream> using namespace std; int main () { int firstvariable; int secondvariable; float float1; float float2; cout << "Please enter two integers and then press Enter:" << endl; cin >> firstvariable; cin >> secondvariable; cout << "Time for some simple mathematical operations:\n" << endl; cout << "The sum:\n " << firstvariable << "+" << secondvariable <<"="<< firstvariable + secondvariable << "\n " << endl; } 
+6
source share
6 answers

You can check the following:

 int x; cin >> x; if (cin.fail()) { //Not an int. } 

Alternatively, you can continue to enter input until you get int via:

 #include <iostream> int main() { int x; std::cin >> x; while(std::cin.fail()) { std::cout << "Error" << std::endl; std::cin.clear(); std::cin.ignore(256,'\n'); std::cin >> x; } std::cout << x << std::endl; return 0; } 

EDIT: To answer the comment below regarding input of type 10abc, one could modify the loop to accept the string as input. Then check the string for any character, not a number, and handle this situation accordingly. In this situation, there is no need to clear / ignore the input stream. Checking a string is just numbers, converting a string back to an integer. I mean, it was just from the cuff. Perhaps the best way. This will not work if you accept float / doubles (you would need to add "." In the search bar).

 #include <iostream> #include <string> int main() { std::string theInput; int inputAsInt; std::getline(std::cin, theInput); while(std::cin.fail() || std::cin.eof() || theInput.find_first_not_of("0123456789") != std::string::npos) { std::cout << "Error" << std::endl; if( theInput.find_first_not_of("0123456789") == std::string::npos) { std::cin.clear(); std::cin.ignore(256,'\n'); } std::getline(std::cin, theInput); } std::string::size_type st; inputAsInt = std::stoi(theInput,&st); std::cout << inputAsInt << std::endl; return 0; } 
+18
source

If istream cannot be inserted, it will set the failure bit.

 int i = 0; std::cin >> i; // type a and press enter if (std::cin.fail()) { std::cout << "I failed, try again ..." << std::endl std::cin.clear(); // reset the failed state } 

You can set this in a do-while loop to get the correct type ( int in this case) correctly inserted.

For more information: http://augustcouncil.com/~tgibson/tutorial/iotips.html#directly

+1
source

Heh, this is an old question that could use a better answer.

User input should be received as a string , and then converted in error to the desired data type. Conveniently, it also allows you to answer questions like "what data type is my input?"

Here is a function that I use a lot. Other options exist, for example, in Boost, but the basic premise is the same: try to convert type string → and watch for success or failure:

 template <typename T> std::optional <T> string_to( const std::string& s ) { std::istringstream ss( s ); T result; ss >> result >> std::ws; // attempt the conversion if (ss.eof()) return result; // success return {}; // failure } 

Using the optional type is one way. You can also throw an exception or return the default value on failure. Whatever works for your situation.

Here is an example of its use:

 int n; std::cout << "n? "; { std::string s; getline( std::cin, s ); auto x = string_to <int> ( s ); if (!x) return complain(); n = *x; } std::cout << "Multiply that by seven to get " << (7 * n) << ".\n"; 

restrictions and type identification

To do this, of course, there must be a method to uniquely extract your data type from the stream. This is the natural order of things in C ++, that is, business as usual. So no surprises here.

The next caveat is that some types combine others. For example, if you are trying to distinguish between int and double , check int first, since everything that converts to int is also double .

+1
source

In c, the isdigit() function is called. It will suit you. Example:

 int var1 = 'h'; int var2 = '2'; if( isdigit(var1) ) { printf("var1 = |%c| is a digit\n", var1 ); } else { printf("var1 = |%c| is not a digit\n", var1 ); } if( isdigit(var2) ) { printf("var2 = |%c| is a digit\n", var2 ); } else { printf("var2 = |%c| is not a digit\n", var2 ); } 

From here

0
source

You can use the variable name to check if the value is an integer. eg:

 #include <iostream> using namespace std; int main (){ int firstvariable; int secondvariable; float float1; float float2; cout << "Please enter two integers and then press Enter:" << endl; cin >> firstvariable; cin >> secondvariable; if(firstvariable && secondvariable){ cout << "Time for some simple mathematical operations:\n" << endl; cout << "The sum:\n " << firstvariable << "+" << secondvariable <<"="<< firstvariable + secondvariable << "\n " << endl; }else{ cout << "\n[ERROR\tINVALID INPUT]\n"; return 1; } return 0; } 
0
source

You can use:

 int a = 12; if (a>0 || a<0){ cout << "Your text"<<endl; } 

I'm sure it works.

-1
source

Source: https://habr.com/ru/post/953590/


All Articles