Get only integers as input, code not working as expected - c++

I am trying to allow only integrers to be inputted
Should Reject:
5h
3.4
3.gh
3.0
htr
Should Accept:
-5
0
78
Current Code
int getIntInput() {
int userInput;
while (true) {
std::cout << "> ";
std::cin >> userInput;
std::cout << std::flush;
if (std::cin.fail()) {
std::string cinBuffer;
std::cin.clear();
std::getline(std::cin, cinBuffer);
continue;
}
break;
}
return userInput;
}
Updated Code
Issues:
Accepts all rejections excluding "htr" (No numerals)
int getIntInput() {
std::string rawInput;
int parsedinput;
while (true) {
std::cout << "> ";
std::getline(std::cin, rawInput);
std::cout << std::flush;
try {
parsedinput = std::stoi(rawInput);
} catch (std::invalid_argument & e) {
continue;
} catch (std::out_of_range & e) {
continue;
}
break;
}
return parsedinput;
}
Finished Code
Accepts only integers with an optional parameter which will allow
negative numbers to be accepted or rejected.
int getIntInput(bool allowNegatives = true) {
bool validIntInput;
std::string rawInput;
int parsedinput;
while (true) {
validIntInput = true;
// Grabs the entire input line
std::cout << "> ";
std::getline(std::cin, rawInput);
std::cout << std::flush;
for (int i = 0; i < rawInput.length(); i++) {
// Checks to see if all digits are a number as well as to see if the number is a negative number
if (!isdigit(rawInput[i]) && !(allowNegatives && i == 0 && rawInput[i] == '-')) {
validIntInput = false;
break;
}
}
if (!validIntInput) {
continue;
} else {
try {
// Try parse the string to an int
parsedinput = std::stoi(rawInput);
// Catch all possible exceptions, another input will be required
} catch (...) {
continue;
}
// If the code reaches here then the string has been parsed to an int
break;
}
}
return parsedinput;}

The way cin works when having to read an int, is that it starts parsing the integer and stops when it finds a non-int character, storing the parsed int in the variable. That's why on float numbers it stops on the dot and in input '5g' it will stop on g.
What you can do instead if you only want integer input, is to read the whole line and then check if every character in your string is numeric, with the following piece of code:
bool onlyNums = true;
for (int i=0;i<rawInput.size();i++) {
if (!isdigit(rawInput[i]))
onlyNums = false;
}
if (!onlyNums)
continue;
(You have to include ctype.h library for the above code)

If you don't mind the overhead, I would grab the input from cin with cin.getline() and save it into a string. Then loop through the string and call isdigit on each char. You can discard the chars that aren't digits by useing the str.erase function.
You will need to #include cctype for isdigit().
Note: this will have at least O(N) runtime based on the length of your string.

template<class T>
T findInStreamLineWith(std::istream &input) {
std::string line;
while (std::getline(input, line)) {
std::istringstream stream(line);
T x;
if (stream >> x >> std::ws && stream.eof()) {
return x;
}
// reenter value
// cout << "Enter value again: ";
}
throw std::invalid_argument("can't find value in stream");
}
…
auto x = findInStreamLineWith<int>(std::cin);

Related

How can I make my else statement account for anything that isn't an integer? [duplicate]

Currently having issues creating an integer validation loop in my code.
Trying to validate for an integer, however this code fails to work if you input, for example the string '22a' it sets trial_no as 22. Is there any way to check that every charachter input is indeed a string, such that '22a' or '2a2' would be considered erroneous and the loop would continue until a valid integer was input?
int trial_no;
bool valid = false;
while(!valid)
{
valid = true; //assume trial_no will be an integer
cout << "Enter the number of die throws to simulate" << endl;
cin >> trial_no;
if(cin.fail()) // exit loop condition is dependent on trail_no being a valid integer
{
cin.clear(); //corrects stream
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skips left over stream data (numeric_limit
// is in case user enters more than one letter))
cout << "Please enter an integer value" << endl;
valid = false; //cin not an integer so loop goes round to try again
}
}
Arguably the best way is to read the entire line as a string and utilize the std::stoi function:
#include <iostream>
#include <string>
int main() {
std::cout << "Enter an integer: ";
std::string tempstr;
bool valid = false;
std::getline(std::cin, tempstr);
try {
int result = std::stoi(tempstr);
std::cout << "The result is: " << result;
valid = true;
}
catch (std::invalid_argument) {
std::cout << "Could not convert to integer.";
valid = false;
}
}
As pointed out in the comments, this function can also throw a std::out_of_range exception. This assumes your compiler is C++11 (+) capable. If not, go down the std::stringstream route:
std::string tempstr;
std::getline(std::cin, tempstr);
std::stringstream ss(tempstr);
int result;
bool valid = false;
if (ss >> result) {
valid = true;
}
else {
valid = false;
}

Transform this function to read the entire file

I made a quiz program in c++. It's working but with separate blocks of instructions for every question in file. I need to transform the block of instructions(after for) to work for all questions.
The file look like this
1.WHEN COMPUTER WAS FIRST INVENTIONED?
a.1822
b.1823
c.1834
d.1922
2.WHO kILLED PRESEDENT BENOGIR VUTTO?
a.nawaz shrif
b.pervase
c.non of them
d.political leder
This is only a function in my program.
void Question::quiz(int &Total)
{
string line[200];
string answer;
string easy[15]={"a","c","a","a","b","c","d","c","a","b","b","c","c","c","a"};
ifstream fin("questions.txt");
if(!fin)
{
cout<<"Cannot open file\n";
exit(1);
}
cout<<"The first question is\n";
for(int contor=0;contor<5;contor++)
{
getline(fin,line[contor]);
cout<<line[contor]<<'\n';
}
cout<<"Select your answer: ";
cin >> answer;
if(answer==easy[0])
{
Total+=1;
}
cin.get();
}
You can use a while loop for reading the file upto end of line. As every block contains exactly five lines, you can take input for each line until you get the line size greater than 0. As blank line will also be in the input and you need to ignore them.
void Question::quiz(int &Total)
{
string line[200];
string answer;
string easy[15]= {"a","c","a","a","b","c","d","c","a","b","b","c","c","c","a"};
ifstream fin("questions.txt");
if(!fin)
{
cout<<"Cannot open file\n";
exit(1);
}
int cnt=0;
while(getline(fin,line[0]))
{
cout<<line[0]<<endl;
while(line[0].size()==0)
{
getline(fin,line[0]);
cout<<line[0]<<endl;
}
for(int contor=1; contor<5; contor++)
{
do
{
getline(fin,line[contor]);
}
while(line[contor].size()==0);
cout<<line[contor]<<'\n';
}
cout<<"Select your answer: ";
cin >> answer;
if(answer==easy[cnt++])total++;
line[0]="";
}
cout<<total<<endl;
}
Here is one was of doing it, making use of objects and vectors:
struct quiz_question
{
auto print_question() -> void
{
char prefix[] = { 'a', 'b', 'c', 'd' };
std::cout << "Question " << question_number << ": " << question << '\n';
for (auto x = 0; x < possible_answers.size(); x++)
{
std::cout << prefix[x] << ". " << possible_answers[x] << '\n';
}
}
auto check_answer(char user_answer) -> bool { return user_answer == answer; }
std::size_t question_number;
std::string question;
std::array< std::string, 4 > possible_answers;
char answer;
};
int main()
{
std::vector< quiz_question > questions;
std::string number_of_questions_str;
std::size_t number_of_questions;
std::ifstream ifs("questions.txt");
if (!ifs)
{
std::cout << "Cannot open questions.txt!\n";
exit(1);
}
// Start loading in questions.
// Read first line, describes how many questions there are.
std::getline(ifs, number_of_questions_str);
ifs.ignore(10000, '\n'); // Ignore a line
try
{
number_of_questions = std::stoul(number_of_questions_str);
}
catch (std::invalid_argument &ec)
{
std::cout << "Unable to parse questions.txt!\n";
exit(1);
}
// Load each question in.
for (auto x = 0; x < number_of_questions; x++)
{
quiz_question current_question;
current_question.question_number = x + 1;
// Read the question line
std::getline(ifs, current_question.question);
// Read the possible answers
for (auto &possible_answer : current_question.possible_answers)
{
std::getline(ifs, possible_answer);
}
// Read the actual answer
current_question.answer = ifs.get();
ifs.ignore(10000, '\n'); // Ignore the rest of that line
questions.push_back(std::move(current_question));
ifs.ignore(10000, '\n'); // Ignore a line
}
// Now all the questions have been loaded. Lets start the quiz!
char answer;
std::size_t score { 0 };
std::cout << "Starting the quiz!\n";
for (auto &question : questions)
{
question.print_question(); // Print question and possible answers
std::cin >> answer;
if (question.check_answer(answer))
{
std::cout << "Correct!\n\n";
score++;
}
else
{
std::cout << "Incorrect!\n\n";
}
std::cin.clear(); // Clear flags
std::cin.ignore(10000, '\n'); // Skip to next line
}
}
I've had to change your questions.txt format a little also, its described below:
questions.txt
2
WHEN COMPUTER WAS FIRST INVENTIONED?
1822
1823
1834
1922
a
WHO KILLED PRESEDENT BENOGIR VUTTO?
nawaz shrif
pervase
non of them
political leder
c
The first line is your total number of questions.
Blank line.
Question line
Answer A
Answer B
Answer C
Answer D
Correct answer
Blank line
Repeat numbers 3-9
Hope this helps

C++ integer validation

Currently having issues creating an integer validation loop in my code.
Trying to validate for an integer, however this code fails to work if you input, for example the string '22a' it sets trial_no as 22. Is there any way to check that every charachter input is indeed a string, such that '22a' or '2a2' would be considered erroneous and the loop would continue until a valid integer was input?
int trial_no;
bool valid = false;
while(!valid)
{
valid = true; //assume trial_no will be an integer
cout << "Enter the number of die throws to simulate" << endl;
cin >> trial_no;
if(cin.fail()) // exit loop condition is dependent on trail_no being a valid integer
{
cin.clear(); //corrects stream
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //skips left over stream data (numeric_limit
// is in case user enters more than one letter))
cout << "Please enter an integer value" << endl;
valid = false; //cin not an integer so loop goes round to try again
}
}
Arguably the best way is to read the entire line as a string and utilize the std::stoi function:
#include <iostream>
#include <string>
int main() {
std::cout << "Enter an integer: ";
std::string tempstr;
bool valid = false;
std::getline(std::cin, tempstr);
try {
int result = std::stoi(tempstr);
std::cout << "The result is: " << result;
valid = true;
}
catch (std::invalid_argument) {
std::cout << "Could not convert to integer.";
valid = false;
}
}
As pointed out in the comments, this function can also throw a std::out_of_range exception. This assumes your compiler is C++11 (+) capable. If not, go down the std::stringstream route:
std::string tempstr;
std::getline(std::cin, tempstr);
std::stringstream ss(tempstr);
int result;
bool valid = false;
if (ss >> result) {
valid = true;
}
else {
valid = false;
}

Restrict spaces from input in console

I have 3 cin for ints.
int input1;
cin >> input;
int input2;
cin >> input2;
int input3
cin >> input3;
The problem is if i type 2 3 4 in console, it will input all 3 in one shot. How can i prevent this? And possibly give them a warning if they do this. basically error input validating.
One possible solution:
int strict_stoi(const string& s)
{
size_t end_pos;
int num = stoi(s, &end_pos);
for (size_t i=end_pos; i<s.length(); ++i)
{
if (!isspace(s[i]))
throw invalid_argument("You have entered some garbage after the number!");
}
return num;
}
int read_number()
{
string s;
getline(cin, s);
return strict_stoi(s);
}
int read_number_with_retry(const char* prompt)
{
for (;;)
{
try
{
cout << prompt;
return read_number();
}
catch (invalid_argument& ex)
{
cout << ex.what() << endl;
}
}
}
int test()
{
int input1 = read_number_with_retry("Enter input #1: ");
int input2 = read_number_with_retry("Enter input #2: ");
int input3 = read_number_with_retry("Enter input #3: ");
return 0;
}
If you enter a totally invalid argument (for example "a") then it will show you a not too user friendly "invalid stoi argument" message but if you enter "5 6" then it shows "You have entered some garbage after the number!". If you want to replace the "invalid stoi argument" message with something user friendly then instead of throwing an invalid_argument exception when you find "garbage after the number" you should throw you own garbage_after_the_number exception and in this case you could make the distinction between two different errors: invalid_argument would be thrown only in case of invalid inputs (like "a") and garbage_after_the_number would be thrown only in case of the other kind of error so you would be able to catch two different exceptions and you could print totally customized message in these two cases. I leave the implementation of this to you as an extra exercise.
You may do:
#include <iostream>
#include <sstream>
int main() {
while(true) {
std::cout << "Enter a number [Enter to quit]: ";
std::string line;
getline(std::cin, line);
if(line.empty()) break;
else {
std::stringstream input(line);
int number;
// Preceding white space number trailing white space:
input >> number >> std::ws;
if(input && input.eof()) {
std::cout
<< "The number surronded by possible white space is: "
<< number
<< '\n';
}
else {
std::cout
<< "The input line is invalid: "
<< line
<< '\n';
}
}
}
}
And if you want to be strict:
#include <iostream>
#include <iomanip>
#include <sstream>
...
// Number without preceding and trailing white space:
input >> std::noskipws >> number;
...

Determining input to be int or char in C++

I'm working on a small program for school to compute a factorial of an inputted number. I've got a good amount of experience in Java, but this is my first go at C++.
My problem: I need to be able to have a single input from the user, which is either an integer or the character 'q', which signifies the application needs to quit.
Here is my current attempt:
#include <stdio.h>
#include <iostream>
#include "Header.h"
using namespace std;
int x=0;
char y;
int main(int argc, char *argv[])
{
printf("Hello, please enter a number to compute a factorial (or 'q' to quit): ");
cin >> y;
x= (y-'0');
if(y=='q')
{ printf("Thanks for playing!\n");
exit(1);
}
long result= print_Factorial(x);
cout << x << "!= " << result << "\n";
return result;
}
This casting does not work, however. If I enter a double digit number, such as 12, it only converts to x the first digit of the two and computes that factorial. I'm sure this is simple, what am I missing?
Explicit answer or a lead to where I can learn more about this problem, anything is appreciated.
There are functions you can use to try and covert a string into a number, and which you can check if the conversion was a success or not. The std::strtol function is one of them:
std::string input;
std::cin >> input;
char* endptr = nullptr;
const char *input_ptr = input.c_str();
long value = std::strtol(input_ptr, &endptr, 10);
if (endptr == input_ptr)
{
// Input was not a valid number
}
else if (*endptr != '\0')
{
// Input starts with a valid number, but ends with some extra characters
// (for example "123abc")
// `value` is set to the numeric part of the string
}
else
{
// Input was a valid number
}
If you don't mind exceptions, then you can use e.g. std::stoi instead:
std::string input;
std::cin >> input;
int value = 0;
try
{
size_t endpos = 0;
value = std::stoi(input, &endpos);
if (endpos != input.length())
{
// Input starts with a valid number, but ends with some extra characters
// (for example "123abc")
// `value` is set to the numeric part of the string
}
else
{
// Input is a valid number
}
}
catch (std::invalid_argument&)
{
// Input not a valid number
}
catch (std::out_of_range&)
{
// Input is a valid number, but to big to fit in an `int`
}
The reason you're getting the first digit is because you're using
cin >> y; where y is a char, which holds one character. So you're only getting one character.
What you probably would want to do is get the answer as a string, and once you check that the string is not == "q", then you could convert it to an int.
#include <iostream>
#include <sstream>
int main() {
std::string in;
std::cout << "Please enter a digit: ";
while(std::cin >> in) {
std::cout << "Input: " << in << std::endl;
if(in.size() == 1) {
if(in[0] == 'q' || in[0] == 'Q') {
std::cout << "Quit" << std::endl;
return 0;
}
}
std::istringstream parse(in);
int value;
if(parse >> value) {
if(parse.eof()) {
std::cout << "Success" << std::endl;
return 0;
}
}
std::cout << "Please try again: ";
}
std::cerr << "This should not happen <control + d>" << std::endl;
return 1;
}
Your user can enter whatever line of text, you you have to read a "line of text" to validate.
#include <iostream>
#include <string>
#include <stdexcept>
int main()
{
std::string text;
std::getline(std::cin,text);
if(text.size()==1 && text[0]=='q')
{
std::cout << "quit command";
return 0;
}
try
{
int i = std::stoi(text); //may throw if text is not convertible
/* whatever elaboration and output */
return 0;
}
catch(const std::exception& e)
{
std::cout << "bad input: " << text << '\n';
std::cout << "caused excpetion: " << e.what() << std::endl;
}
return 3; //means "excpetion thorown"
}