How can I validate an integer input [duplicate] - c++

This question already has answers here:
Integer validation for input
(3 answers)
Closed 6 years ago.
For example I have the code:
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "Please input a number:";
while(!(cin >> test))
{
cout << "Invalid input";
}
if(test == 1)
{
cout << "Test is 1";
}
else
{
// Do something
}
return 0;
}
If I input 1abc to the test variable it still continues to process the if statement even though the input is wrong. How could I ignore all the input made and just accept pure numbers in it?

You can use getline, find_if_not, and isdigit to check if an entire line is a valid integer or not. This will loop until it reads an integer:
std::string number;
while (std::getline(std::cin, number) && number.end() !=
std::find_if_not(number.begin(), number.end(), &isdigit))
std::cout << "gitgud!";
getline will read the input up to the newline, and put it in the string. find_if_not and isdigit will find the first non-digit character. If there are none (meaning it is a valid integer), it will return the end iterator.

Related

How do I put an error message to this c++ code? [duplicate]

This question already has answers here:
Integer validation for input
(3 answers)
Closed 6 years ago.
For example I have the code:
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "Please input a number:";
while(!(cin >> test))
{
cout << "Invalid input";
}
if(test == 1)
{
cout << "Test is 1";
}
else
{
// Do something
}
return 0;
}
If I input 1abc to the test variable it still continues to process the if statement even though the input is wrong. How could I ignore all the input made and just accept pure numbers in it?
You can use getline, find_if_not, and isdigit to check if an entire line is a valid integer or not. This will loop until it reads an integer:
std::string number;
while (std::getline(std::cin, number) && number.end() !=
std::find_if_not(number.begin(), number.end(), &isdigit))
std::cout << "gitgud!";
getline will read the input up to the newline, and put it in the string. find_if_not and isdigit will find the first non-digit character. If there are none (meaning it is a valid integer), it will return the end iterator.

Input validation: need integer but string is entered. C++ [duplicate]

This question already has answers here:
Integer validation for input
(3 answers)
Closed 6 years ago.
For example I have the code:
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "Please input a number:";
while(!(cin >> test))
{
cout << "Invalid input";
}
if(test == 1)
{
cout << "Test is 1";
}
else
{
// Do something
}
return 0;
}
If I input 1abc to the test variable it still continues to process the if statement even though the input is wrong. How could I ignore all the input made and just accept pure numbers in it?
You can use getline, find_if_not, and isdigit to check if an entire line is a valid integer or not. This will loop until it reads an integer:
std::string number;
while (std::getline(std::cin, number) && number.end() !=
std::find_if_not(number.begin(), number.end(), &isdigit))
std::cout << "gitgud!";
getline will read the input up to the newline, and put it in the string. find_if_not and isdigit will find the first non-digit character. If there are none (meaning it is a valid integer), it will return the end iterator.

How to clear output buffer for int [duplicate]

This question already has answers here:
How to test whether stringstream operator>> has parsed a bad type and skip it
(5 answers)
Closed 2 years ago.
Im a new user of c++ and I am stuck with a problem.
If the input for the int variable 'x' is anything other than a number, then c++ seems to skip the cin associated with the char variable 'y'. I tried to use cin.ignore and cin.clear, but they dont seem to work. Any ideas on how to make the program still ask the value for char 'y' when the value for int 'x' is anything but a number(eg. 'a' '*' '') ?
#include <iostream>
using namespace std;
int main()
{
int x;
char y;
cout << "num: ";
cin >> x;//int input
cout << endl;//blank line
cout << "char: ";
cin >> y;//char input
cout << endl;//blank line
return 0;
}
Are you passing parameters to ignore?
The first parameter is the maximum of chars to be ignored and the second one is the flag that it should stop ignoring after findind it (which in the example below I've put '\n')
cin.clear();
cin.ignore(1000, '\n');
To summarize, the code says: Clear up to 1000 chars or up to the first '\n'.

C++ Phone number validation [duplicate]

This question already has answers here:
Integer validation for input
(3 answers)
Closed 6 years ago.
For example I have the code:
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "Please input a number:";
while(!(cin >> test))
{
cout << "Invalid input";
}
if(test == 1)
{
cout << "Test is 1";
}
else
{
// Do something
}
return 0;
}
If I input 1abc to the test variable it still continues to process the if statement even though the input is wrong. How could I ignore all the input made and just accept pure numbers in it?
You can use getline, find_if_not, and isdigit to check if an entire line is a valid integer or not. This will loop until it reads an integer:
std::string number;
while (std::getline(std::cin, number) && number.end() !=
std::find_if_not(number.begin(), number.end(), &isdigit))
std::cout << "gitgud!";
getline will read the input up to the newline, and put it in the string. find_if_not and isdigit will find the first non-digit character. If there are none (meaning it is a valid integer), it will return the end iterator.

How to accept only numbers in 'cin' (C++ Programming)? [duplicate]

This question already has answers here:
How to make cin take only numbers
(2 answers)
Closed 6 years ago.
I have the following code:
#include <iostream>
#include <limits>
#include <locale>
using namespace std;
int main(int argc, char** argv) {
setlocale(LC_ALL, "English");
int i, vetor[16];
for(i = 0; i < 16; i ++) {
cout << "Input the position: [" << i << "]: ";
cin >> vetor[i];
if(cin.fail()) {
cin.clear();
cin.ignore(numeric_limits < streamsize > ::max(), '\n');
cout << "Please, input only numbers." << endl;
i --;
continue;
}
if(vetor[i] <= 0) {
cout << "Insert a non-zero value." << endl;
i --;
continue;
}
}
cout << vetor[0];
return 0;
}
And I need to accept only numbers. When I run it and insert "1" (or any other number), it goes to the following position, what's correct. When I input "a", it shows me the error and warn me to input only numbers, what's correct too. If I input "a1", the same thing occurs, what's correct. But when I input "1a", it shows me the warning, but the code continues to the next position, and when it executes the last line with the cout command above the return, it tells me the value is "1", not "1a", because the type of the variable is int I believe.
Can someone tells me why does it happen? I need to accept only numbers, and "1a" is not a number. How can I filter this and how can I do to when I input "1a", occurs the same thing as I had input "a"?
I use DevC++ 5.11.
It happens because cin extracts the 1 digit in one pass, and stores that in your array. It stops reading when it encounters the a (since that is not a valid character for an integer), and leaves it in the input buffer. It then reads the a on the next pass, which causes it to fail because it's not an integer.
What you should do is use std::getline to read strings instead. Then you can do any kind of parsing you want. You can use functions from the standard library like std::stoi if they suit you. Otherwise write your own custom parsing.