I basically want to validate that I have an int and not a floating point number. What I currently have is:
int den1;
cout << "Enter denominator of first fraction" << endl;
cin >> den1;
while (den1 == 0){
cout << "Enter a non-zero denominator" << endl;
cin >> den1;
}
Is there a "test" to generate a boolean value for den1 == int? I'm trying to avoid using getline() because I don't want to use a string if it isn't necessary.
If you want to force your input to be of an integer type, then use an integer type for your input. If den1 is an int, it will not let you put a floating point value in it. That is, cin >> den1 will be an int value. If the user tries to input 3.14159, only the 3 will be read (it will stop reading at the .. Note that the rest of the buffer will contain numbers as well, so if you don't clear it, the next attempt to read an integer will read 14159.
EDIT
If you want to "force" the user to enter a valid integer, you can do something like this:
std::string line;
int value = 0;
bool valid = false;
do
{
if (std::getline(std::cin, line))
{
if (std::string::npos == line.find('.'))
{
// no decimal point, so not floating point number
value = std::stol(line);
valid = true;
}
else
{
std::cin.clear();
}
}
} while (!valid);
Which is a lot of extra code compared to:
int value;
std::cin >> value;
You want to use something like
if (std::cin >> den) {
// process den
}
else {
// deal with invalid input
}
When an input operation fails, it sets std::ios_base::failbit on the stream and the stream converts to false instead of true. While the stream is in this failure mode, it won't read anything from the stream, i.e., the failure mode as to be cleared, e.g., using
std::cin.clear();
Once the failure mode is cleared, the offending character still sits in the stream. You can ignore the next character using, e.g.
std::cin.ignore();
or ignore all characters until the next newline:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
First of all, user input is always a string. Next, you need to define your goal more precisely. For example a reasonable thing to distinguish is whether the input can be parsed in its entirety as an integer, or as a floating point number, or neither. Here's one way to do this with iostreams, disregarding whitespace:
#include <iostream>
#include <sstream>
#include <string>
for (std::string line; std::getline(std::cin, line); )
{
std::istringstream iss1(line), iss2(line);
int n;
double x;
if (iss1 >> n >> std::ws && iss1.get() == EOF)
{
// have an int, use "n"
}
else if (iss2 >> d >> std::ws && iss2.get() == EOF)
{
// have a floating point number, use "d"
}
else
{
// failed to parse the input
continue;
}
}
Related
In my case, I have to make sure the user input is either 1 or 2, or 3.
Here's my code:
#include <iostream>
using namespace std;
void invalid_choice_prompt() {
string msg = "\nInvalid Command! Please try again.";
cout << msg << endl;
}
int ask_user_rps_check_input(int user_choice) {
if (user_choice == 1 || user_choice == 2 || user_choice == 3) return 1;
return 0;
}
int ask_user_rps() {
// ask user's choice of Rock or Paper or Scissors
while (1) {
string msg =
"\nPlease enter your choice:\nRock - 1\nPaper - 2\nScissors - 3";
cout << msg << endl;
int user_choice;
cin >> user_choice;
if (ask_user_rps_check_input(user_choice)) {
return user_choice;
}
invalid_choice_prompt();
}
}
int main() {
ask_user_rps();
return 0;
}
The code is capable to handle the situation when the input is an integer, but when the input are characters or strings, the program will be trapped in the infinite loop.
Is there any elegant way to do this? I've found some methods about using cin.ignore to ignore the specified length of io buffer, but I don't think this method is flexible enough. I am looking for a more flexible solution.
I think an option would be to collect the user input to a string and then move it to stringstream using getline kind of like this:
std::string input;
std::getline(std::cin, input);
//Now check if the input is correct. if it is, then:
std::stringstream stream;
stream << input;
int num;
stream >> num;
I'm not sure if this is a good method but it works.
One of the simplest solution would be to check the cin stream failure something like below:
int ask_user_rps() {
// ask user's choice of Rock or Paper or Scissors
while (1) {
string msg =
"\nPlease enter your choice:\nRock - 1\nPaper - 2\nScissors - 3";
cout << msg << endl;
int user_choice;
cin >> user_choice;
if(cin.fail()) {
invalid_choice_prompt();
std::cin.clear();
std::cin.ignore(256,'\n');
continue;
}
if (ask_user_rps_check_input(user_choice)) {
return user_choice;
}
invalid_choice_prompt();
}
}
Reading from a stream using operator >> takes as many characters from the stream as the target type accepts; the rest will remain in the stream for subsequent reads. If the input has a format error (e.g. a leading alphabetical characters when an integer is expected), then an error-flag is set, too. This error-flag can be checked with cin.fail(). It remains set until it gets explicitly cleared. So if your code is...
int user_choice;
cin >> user_choice;
and if you then enter something that is not a number, e.g. asdf, then user_choice has an undefined value, an error-flag cin.fail() is (and reamins) set. So any subsequent read will fail, too.
To overcome this, you have to do three things:
First, check the error-flag. You can do this either through calling cin.fail() after a read attempt of through checking the return value of the expression (cin >> user_choice), which is the same as calling cin.fail().
Second, in case of an error, you need to clear the error-flag using cin.clear(). Otherwise, any attempt to read in anything afterwards will fail.
Third, if you want to continue with reading integral values, you need to take the invalid characters from the stream. Otherwise, you will read in asdf into a variable of type integer again and again, and it will fail again and again. You can use cin.ignore(numeric_limits<streamsize>::max(),'\n'); to take all characters until EOF or an end-of-line from the input buffer.
The complete code for reading an integral value with error-handling could look as follows:
int readNumber() {
int result;
while (!(cin >> result)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Input is not a number." << std::endl;
}
return result;
}
Take input as char
string user_choice;
cin >> user_choice;
check input is valid or not if(user_choice=='1')
This function keeps getting called in another function inside a while-loop while valid_office_num is false. The problem is that if the input begins with a digit but is followed by other invalid characters (e.g. 5t) it takes the digit part and accepts that as a valid input. I want it to consider the whole input and reject it so it can ask for another one. I thought I could use getline() but then I cannot use cin.fail(). How could I implement this behavior?
I forgot to mention I am very new to C++, I have only learnt the basics so far.
(To be clear the desired behavior is to reject anything that contains anything other than digits. This is not an integer range check question. If it is NOT an integer, discard it and request another one)
//Function to read a valid office number, entered by user
int read_office_num()
{
//Declaration of a local variable
int office_num;
//Read input
cin >> office_num;
//Check if input was valid
if (cin.fail())
{
//Print error message
cout << "\nInvalid office number, it should only consist of digits!! Enter another:\n";
//Clear error flags
cin.clear();
//Ignore any whitespace left on input stream by cin
cin.ignore(256, '\n');
}
else
{
//Office number entered is valid
valid_office_num = true;
}
return office_num;
}
From what I gather you want the whole line to be read as a number and fail otherwise?
Well, you can use std::getline(), but you have to follow the algorithm below (I will leave the implementation to you..)
use std::getline(cin, str) to read a line, and if this returns true
use std::stoi(str, &pos) to convert to integer and get the position of the last integer
if pos != str.size() then the whole line in not an integer (or if the above throws an exception), then it's not a valid integer, else return the value...
Read a line of input as a std::string using std::getline().
Examine the string and check if it contains any characters that are not digits.
If the string only contains digits, use a std::istringstream to read an integer from the string. Otherwise report a failure, or take whatever other recovery action is needed (e.g. discard the whole string and return to read another one).
You could use a stringstream
int read_office_num()
{
//Declaration of a local variable
int office_num;
string input = "";
while (true) {
getline(cin, input);
stringstream myStream(input);
if (myStream >> office_num)
break;
cout << "\nInvalid office number, it should only consist of digits!! Enter another:\n" << endl;
}
return office_num;
}
If you want to reject input like 123 xxx you could add an additional check to verify that the received string is indeed an integer:
bool is_number(const string& s)
{
string::const_iterator itr = s.begin();
while (itr != s.end() && isdigit(*itr)) ++itr;
return !s.empty() && itr == s.end();
}
int read_office_num()
{
//Declaration of a local variable
int office_num;
string input = "";
while (true) {
getline(cin, input);
stringstream myStream(input);
if (is_number(input) && myStream >> office_num)
break;
cout << "\nInvalid office number, it should only consist of digits!! Enter another:\n" << endl;
}
return office_num;
}
You should probably just look at the number of input characters that are left in cin. You can do that with in_avail
Your function will probably end up having a body something like this:
//Declaration of a local variable
int office_num;
//Read input and check if input was valid
for (cin >> office_num; cin.rdbuf()->in_avail() > 1; cin >> office_num){
//Print error message
cout << "\nInvalid office number, it should only consist of digits!! Enter another:\n";
//Ignore any whitespace left on input stream by cin
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
//Office number entered is valid
valid_office_num = true;
return office_num;
Points of interest:
There is always at least 1 character in cin otherwise the cin would be marked as bad and that would not be good
You don't need valid_office_num if read_office_num is implemented this way, cause valid_office_num will always be set to true before returning
Hm. I may be missing something, but why not read a line, trim it, use regular expressions to validate a number and then exploit strstream's facilities or just atoi if you must? In all reality I'd probably just let users get away with extraneous input (but discard it if I'm sure I'm always running interactively). Following the motto "be lenient in what you accept."
The "interactive" caveat is important though. One can generally not assume that cin is a terminal. Somebody may get cocky and let your program run on a text file or in a pipeline, and then it would fail. A robust approach would separate data processing (portable) from means of input (possibly machine specific and therefore also more powerful and helpful than stdin/stdout via a console).
Here's how to do it using Boost Lexical Cast:
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <vector>
#include <string>
int read_office_num()
{
using boost::lexical_cast;
using boost::bad_lexical_cast;
using namespace std;
int office_num;
while (true)
{
try
{
string input = cin.getline();
office_num = lexical_cast<int>(*argv));
break;
}
catch(const& bad_lexical_cast)
{
cout << "\nInvalid office number, it should only consist of digits!! Enter another:\n";
}
}
return office_num;
}
I ask the user for an integer input and I do not want to execute code unless it is strictly an integer.
int x;
if(cin >> x)
For instance if the user inputs a double above, the if statement will execute with implicit conversion to an integer. Instead I don't want the code to execute at all.
How can I prevent this?
There is no conversion there. If the user enters a fraction (there is no double), then the >> extraction stops at the decimal point.
http://ideone.com/azdOrO
int main() {
int x;
std::cin >> x;
std::cout << std::cin.rdbuf();
}
input:
123.456
output:
.456
If you want to flag the existence of the decimal point as an error, you will have to do something to extract it from cin and detect it.
One good parsing strategy with C++ streams is to getline what you know you will process into an istringstream, call it s, then check that that s.peek() == std::char_traits<char>::eof() when you finish. If you don't use getline to pull the individual number, then peek can check whether the next character is a space (using std::isspace) without consuming that character from the stream.
Probably the cleanest way to check that input is finished, although it's somewhat esoteric, is to use std::istream::sentry.
if ( ! ( std::cin >> x ) || std::istream::sentry( std::cin ) ) {
std::cerr << "Invalid or excessive input.\n";
}
This consumes space at the end of the input. sentry also provides a noskipws option to avoid consuming the space.
if ( ! ( std::cin >> x ) || std::istream::sentry( std::cin, true ) ) {
std::cerr << "Invalid or excessive input. (No space allowed at end!)\n";
}
This seems to work. It ignores whitespace, I don't know if that's ok with you.
string s;
cin >> s;
stringstream ss(s);
int x;
if (! (ss >> x))
{
cerr << "You didn't enter an integer." << endl;
return -1;
}
string temp;
ss >> temp;
if (! temp.empty())
{
cerr << "You didn't enter an integer." << endl;
return -1;
}
Ok, I'm trying to get good at using pointers so I'm trying to write a input validation for the user input to make sure that anything that isn't a number is handled correctly. When I use isdigit() isn't working for me. I still get an exception when I enter a alphabet. Any suggestions? Thanks. Check this out:
#include<iostream>
#include<algorithm>
#include<string>
#include<cctype>
using namespace std;
void EnterNumbers(int * , int);
int main()
{
int input = 0;
int *myArray;
cout << "Please enter the number of test scores\n\n";
cin >> input;
//Allocate Array
myArray = new int[input];
EnterNumbers(myArray,input);
delete[] myArray;
return 0;
}
void EnterNumbers(int *arr, int input)
{
for(int count = 0; count < input; count++)
{
cout << "\n\n Enter Grade Number " << count + 1 << "\t";
cin >> arr[count];
if(!isdigit(arr[count]))
{
cout << "Not a number";
}
}
}
If you test if (!(cin >> arr[count])) ... instead - isdigit(arr[digit]) tests if the value of arr[digit] is the ASCII code of a digit [or possibly matches Japanese, Chinese or Arabic (that is, as an Arabic script typeface, not that it's a 0-9 like our "Arabic" ones) digit]. So if you type in 48 to 57, it will say it's OK, but if you type 6 or 345, it's complaining that it is not a digit...
Once you have discovered a non-digit, you will also need to either exit or clean out the input buffer from "garbage". cin.ignore(1000, '\n'); will read up to the next newline or a 1000 characters, whichever happens first. Could get annoying if someone has typed in a million digits, but otherwise, should solve the problem.
You will of course also need a loop to read the number again, until a valid number is entered.
The way I do this kind of input validation is that I use std::getline(std::cin, str) to get the whole line of input and then I parse it using the following code:
std::istringstream iss(str);
std::string word;
// Read a single "word" out of the input line.
if (! (iss >> word))
return false;
// Following extraction of a character should fail
// because there should only be a single "word".
char ch;
if (iss >> ch)
return false;
// Try to interpret the "word" as a number.
// Seek back to the start of stream.
iss.clear ();
iss.seekg (0);
assert (iss);
// Extract value.
long lval;
iss >> lval;
// The extraction should be successful and
// following extraction of a characters should fail.
result = !! iss && ! (iss >> ch);
// When the extraction was a success then result is true.
return result;
isdigit() applies to char not to int as you're trying. The cin >> arr[count]; statement already ensures an integer numeric digits format is given in the input. Check cin.good() (!cin respectively) for possible input parsing errors.
How can I restrict the user to input real numbers only in C++ program?
Example:
double number;
cin >> number;
and it won't accept the input like: '12add' , 'abcd' etc...
can someone guides me to that? using bool value.
Thanks!
You cannot force the user to give correct input. But you can ask them to give another input if previous was invalid. There are different procedures to do so. One is the following:
Use getline to read a line
Parse and understand the line
If line is invalid, give error to user and go to 1
This is alright and quite common. It uses dynamic memory though. Another option would be:
Use cin >> value; like you normally do
Check cin.fail() to see if input was correctly read (check for cin.eof() also)
If failed, ignore all input until whitespace:
char c;
while (cin >> c)
if (isspace(c))
break;
This has the added advantage that in an erroneous input like this:
abc 12.14
you don't ignore the whole line, but just the abc.
I always use this code to request a specific type of input(Except strings and chars).
The idea is to request any numeric type and use stringstream to see if it can be stored as the requested type, if not it will keep prompting the user until he inputs the requested type.
template <typename T> // will not work with strings or chars
T forceInputType_T() {
T name;
bool check = false;
string temp;
while (check == false) {
cin >> temp;
stringstream stream(temp);
if (stream >> number) {
check = true;
} else {
cout << "Invalid input type, try again..." << endl;
}
}
return name;
}
If you want to use a Boolean then you could check every character in the string if it contains a number than return false and keep asking for an valid input with a loop !
You cannot restrict what user types on the keyboard. You can accept it as std::string and use boost::lexical_cast to convert it to your expected number type and catch and process boost::bad_lexical_cast exception.
You can retrieve your data as a std::string then use one of the standard string conversion function to see if the content matches your expectations.
double number
if (cin >> number) {
do_stuff_with(number);
} else {
std::cerr << "That wasn't a number!";
}
Check out the sscanf function.
Unfortunately you cannot avoid it... You can accept a string as input and parse the string (maybe with regex) for correctness.
You can use regex to solve it
double inputNumber()
{
string str;
regex regex_double("-?[0-9]+.?[0-9]+");
do
{
cout << "Input a positive number: ";
cin >> str;
}while(!regex_match(str,regex_double));
return stod(str);
}
Remember that include regex library in the header.
Use this:
#include <conio.h>
#include <string>
#include <iostream>
using namespace std;
int main() {
cout << "Input a positive whole integer: ";
string currentInput;
while (true) {
char ch = getch();
if (ch <= '9' and ch >= '0') cout << ch; currentInput += ch;
// Handle other keys (like backspace, etc)
else if (ch == '\r') cout << endl; break;
}
}