Input validation: need integer but string is entered. C++ [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

not taking input of second string [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 1 year ago.
Hi I am solving a question of the book C++ Primer by Stanely. The questions is as follows :-
Write a program to read two strings and report whether the
strings are equal. If not, report which of the two is larger. Now, change
the program to report whether the strings have the same length, and if
not, report which is longer.
I have used a variable choice to switch between the program i.e whether to check strings are equal or not. Or to check whether the strings have same length or not.
#include<iostream>
using namespace std;
int main(){
char choice;
cout<<"Please enter choice"<<endl<<"For Larger press (L) and for longer press (l) "<<endl;
cin>>choice;
string s1, s2 ;
getline(cin,s1);
getline(cin,s2);
if(choice=='L'){
if(s1!=s2){
if(s1>s2) {
cout << "string which is larger is : " <<s1<<endl;
}
else{
cout<<"string which is larger is : " <<s2<<endl;
}
}
else{
cout<<"Both strings are equal "<<endl ;
}
}
else if (choice == 'l'){
if(s1.size() != s2.size()){
if(s2.size()> s1.size()){
cout<<"Longer string : "<<s2<<endl;
}
else {
cout<<"Longer string : " << s1<<endl;
}
}
else {
cout<<"Both strings have same length" <<endl;
}
}
else{
cerr<<"wrong input!! "<<endl;
return -1;
}
return 0;
}
but when I'm compiling the program, It is only taking input of string s1 and not taking input of string s2.
The output is as follows :-
Apparently using cin>> leaves out the '\n' from the input that is absorbed by the first getline() that, as #Scheff'sCat said, reads everything until '\n'. This means that the first getline() exits right away and shows only the second one.
You can try to use cin.ignore('\n').

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.

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.

How can I validate an integer input [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.