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

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.

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.

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 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.

Converting char to int when asked to enter an integer and checked with isdigit

So recently, I came across using isdigit as a way to check to see if an entered value for an int is actually an integer, rather than a string or a char.
However, when I wrote a short program to play around with that, the program failed to execute from that point on.
EDIT: I also in the program wanted to take the invalid data and convert it to a different data type.
Here is the code:
#include <iostream>
#include <sstream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
int enterCFN;
char revisit;
int review(0);
cout << "Enter a digit: ";
cin >> enterCFN;
bool y = isdigit(enterCFN);
if (y == false)
{
// This is the data conversion section
revisit = enterCFN;
revisit = review;
cout << review << "\n";
}
else
{
cout << enterCFN << "\n";
}
return 0;
}
Is there anyone who can correct my error and show me what I'm doing wrong?
enterCFN is an int. It stores a number. isdigit() checks if a character represents a number. These are not the same thing: for example 32 is a number but char(32) means ' ' (space).
What you want instead is this:
if (cin >> enterCFN)
That will take the input from the user and check if it is valid all at once. No need for isdigit().
isdigit() checks if a given character is one of 0-9
For validating integer do something like following:
std::cout << "Enter a digit: ";
std::cin >> enterCFN ;
while (1)
{ if ( std::cin >> enterCFN )
{
// good input
break ;
}
else
{
std::cout << "Enter a digit: ";
// clear stream flags set due to bad input
std::cin.clear();
// get rid of the bad input.
// ignore the rest of the line
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}