c++ - Input Validation Putting Numbers Into Vector - c++

I have been trying to create a simple while loop to cin numbers into a vector, with some basic input validation. However, I am finding some weird things happening with my code. I am trying to have it so that the user can input numbers until they type in '0', which will then end the loop. This works fine.
However, I am also trying to make it so that if they enter a non-int (i.e. 3.4, a), or if they enter a negative number, it couts "BAD INPUT" and then quits the program. When I type in a negative number, it works fine. But when I type in a non-int, it does not cout anything, but still quits the program...
Likewise, I am also trying to have it so that if they type in '0' first, without having put in any numbers beforehand, it couts "NO NUMBERS" and quits the program. This one also quits the program, but again, does not cout anything.
My code is as follows:
#include <iostream>
#include <vector>
//I know it is considered bad practice for the below part.
using namespace std;
int main()
{
vector<int> numberStorage;
while (cin) {
int numInput = 0;
cin >> numInput;
if (numInput == 0) break;
if (!cin || numInput < 0) {
cout << "BAD INPUT" << '\n';
return false;
}
if (numInput == 0 && numberStorage.size() == 1) {
cout << "NO NUMBERS" << '\n';
return false;
}
numberStorage.push_back(numInput);
}
return 0;
}
Can anyone help me and clarify as to where my logic/code is going wrong here on these input validations? Thank you for any help.

Try this:
#include <iostream>
#include <vector>
//I know it is considered bad practice for the below part.
using namespace std;
vector<int> numberStorage;
int main() {
while (true) {
int numInput = 0;
cin >> numInput;
if (cin.fail() || numInput < 0) {
cout << "BAD INPUT" << '\n';
break;
}
if (numInput == 0) {
cout << "Received 0" << endl;
break;
}
if (numInput == 0 && numberStorage.size() == 1) {
cout << "NO NUMBERS" << '\n';
return false;
}
numberStorage.push_back(numInput);
}
return 0;
}
In the original code, you were not checking for cin.fail().
When you were trying to store a value that was not an int inside numInput,
the cin was silently failing and as you initialized numInput to 0, it was exiting the loop as if the client had entered the number zero on console.
You can check that yourself by adding a cout on the zero value condition of the original code.
cheers

When cin fails, it will assign value 0 to numInput and your logic
if (numInput == 0) break;
silently exiting the program.

It would be a good practice if you call cin.clear() in case cin.fail() occurs in order to clear the error flag on cin, so that future I/O operations will work correctly.
Just a recommendation!

You can check by using cin.fail() just after setting integer for checking not number.
#include <iostream>
#include <vector>
using namespace std;
vector<int> numberStorage;
int main() {
while (cin) {
int numInput = 0;
cout << "Please enter:";
cin >> numInput;
if(cin.fail()) {
cout << "NO NUMBER" << "\n";
}
if (numInput == 0) break;
if (!cin || numInput < 0) {
cout << "BAD INPUT" << '\n';
return false;
}
numberStorage.push_back(numInput);
}
}
here is the result : https://onlinegdb.com/SyIWdqU9m

Related

How can i check a variable type in a conditional statement in c++?

I am pretty new to c++ and im having an issue trying to get my program out of a loop when a string is entered for the variables cont, and answer. In python it is pretty easy to do simple checks but I am not sure what I should be doing in cpp. I tried doing a check using if(typeid(answer)) == typeid(string)) but this doesnt work. I havent tried putting a check for
'y'||'Y'||'n'||'N' for cont but im assuming it would be something like that? just check for those 4 characters?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main() {
unsigned seed;
char cont = 'y';
int answer = 0;
seed = time(nullptr);
srand(seed);
rand() % 100 + 1;
cout << "Lets play a math game!\n";
while(cont == 'y')
{
int num1 = rand() % 100 + 1;
int num2 = rand() % 100 + 1;
cout << "What is the result of this addition? \n " << num1 << '\n' << "+" << num2 << endl;
cin >> answer;
if (typeid(answer)==typeid(string))
{
while(typeid(answer) == typeid(string))
{
cout << "Please enter an integer!" << endl;
cin >> answer;
}
}
else if (typeid(answer) == typeid(int)) {
if (answer == (num1 + num2)) {
cout << "You are correct, would you like to play again?" << endl;
cin >> cont;
} else {
cout << "You were incorrect, would you like to try again? enter y/n" << endl;
cin >> cont;
}
} else {
answer = 0;
cout << "You did not enter an integer!\n" << endl;
cout << "Would you like to try again?" << endl;
}
}
return 0;
}
How can i check a variable type in a conditional statement in c++?
You do that already, though I'd do this instead:
#include <type_traits>
#include <iostream>
int main() {
int answer =0;
if constexpr(std::is_same_v<int,decltype(answer)>) {
std::cout << "answer is indeed an int";
}
}
However, this will always print the expected answer is indeed an int, because answer is an int not something else. If the user enters invalid input the variable answer declared as int will not turn into a std::string.
would something like if(inRange(0,200,answer)) work?
No it would not. std::cin >> answer; either succeds to read a number, or it fails and then 0 is assigned to answer. You cannot decide if valid input was entered by looking at answer only.
To check if the user entered valid input you can check the state of the stream:
#include <iostream>
#include <limits>
int main() {
int answer =0;
while(!(std::cin >> answer)){
std::cout << "please enter a number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << answer;
}
Note that this accepts for example 42asdf as valid input, because std::cin >> answer does read 42 before it encounters something that is not a number. For something more sophisticated you can read a std::string and parse that.

cin buffer Issues in C++

I'm learning C++, so I don't fully understand what's going on with my code here, but from what I've been able to glean, it seems like it could be some kind of buffer issue.
#include <stdio.h>
#include <vector>
#include <iostream>
#include <typeinfo>
using namespace std;
bool stopRun = true;
int height, average, total, count;
vector <int> heights;
int main ()
{
while (stopRun)
{
cout << "Enter a height, or 'end' to quit: ";
cin >> height;
if (typeid(height).name() == "i")
{
heights.push_back(height);
cout << heights[0];
count++;
}
else if (typeid(height).name() == "i")
{
cout << "\nPlease enter an integer: ";
continue;
}
if (count == 5)
{
stopRun = false;
}
}
for (int i = 0; i < heights.size(); i++)
{
total += heights[i];
cout << "\nTotal: " << total;
}
return 0;
}
For some reason, this code will continuously output: "Enter a height, or 'end' to quit: ". In an earlier version, it would output: "Enter a height, or 'end' to quit: Please enter an integer: ".
What I think is going on is that my "cin >> height;" line is pulling in the output from "Please enter an integer: " and treating it as my input, which identifies it as not being of type integer, which starts the infinite loop.
How do I clear the input buffer so that it doesn't bring in cout statements? Or is that even the issue I'm experiencing here?
Thanks in advance!
I suggest to catch the string. If string is not "end" then convert to number inside try/catch
you can use this function at the start of your program fflush(stdin). It will clear your input buffer.
You are attempting to read an int and a string in the same line of code. I suggest you use getline() to read the input and try to convert the string to int.
std::string input;
while (heights.size() != 5) {
cout << "Enter a height, or 'end' to quit: ";
if (std::getline(cin, input)) {
if (input == "end") break;
try {
heights.push_back(std::stoi(input));
}
catch (std::invalid_argument e) {
cout << "\nPlease enter an integer: ";
}
}
}
if (string(typeid(height).name()) == "i")
What you had wrong was the comparison of a pointer and string. Since typeid(height).name() returns a pointer to a c-string with the name for the object.

How to prompt user to re-loop the whole program?

I want the user to choose between playing the game again or ending the program, however when prompted, if they press 'y' the same thing gets repeated over and over instead of the whole program from the very beginning. I've tried while loops, do/while loops, if statements, rearranging the code, but nothing has worked. Any advice?
#include <iostream>
#include <string>
using namespace std;
int main(){
string animal = "fish";
string guess;
char choose = 'Y' ;
int count = 0;//keeps a running total of how many times the user
has guessed an answer.
int limit = 5;//allows user to guess only 5 times, otherwise
they loose the game.
bool out_of_guesses = false;//to check whether the user has run
out of guesses.
cout << "I am thinking of an animal.\n" << endl;
do{
while(animal != guess && !out_of_guesses){//Nested while
loop inside main loop to keep track of how many tries the user has
attempted and to validate their answers.
if(count < limit){
cout << "Can you guess what animal I am thinking of?: ";
getline(cin, guess);
count++;
if(animal != guess){
cout << "\nHmm, nope. That's not the animal I'm
thinking of." << endl;
if(count > 2 && count <5){
cout << "I'll give you a hint. It lives in
water." << endl;
}
}
}
else{
out_of_guesses = true;
}
}//End nested while loop
if(out_of_guesses){
cout << "\nI'm sorry, but you are out of guesses." <<
endl;
}
else{
cout << "\n*** Good job! You guessed the correct animal!
***" << endl;
cout << "\t\t><)))º> ❤ <º)))><\t\t" << endl;
}
//The do-while loop is there to ask the user if they wish to
play the game again.
cout << "Would you like to try again?(y/n): ";
cin >> choose;
if(choose == 'N' || choose == 'n')
break;
}while(choose == 'Y' || choose == 'y');
return 0;
}
The bool out_of_guesses = false; must be in-between while(true) and while(animal != guess && !out_of_guesses), and not outside the first while loop. Because our while loop condition is always false, and then it does enter it.
You should also reset your guess variable in-between those 2 loops, else same thing could happen (false while loop) in case of the answer is found.
Here the code with some refactoring/review, which I used the guess as upper case to handle any typography of the answer. I also removed the out of guess variable to use the count and limit one instead.
#include <iostream>
#include <string>
#include <cctype>
int main()
{
const std::string animal = "FISH";
const int limit = 5;
do
{
std::cout << "I am thinking of an animal.\n";
int count = 0;
std::string guess;
while(animal.compare(std::toupper(guess)) != 0 && count < limit)
{
std::cout << "Can you guess what animal I am thinking of?: \n";
std::cin >> guess;
count++;
if(animal.compare(std::toupper(guess)) != 0)
{
std::cout << "\nHmm, nope. That's not the animal I'm thinking of.\n";
if(count > 2)
{
std::cout << "I'll give you a hint. It lives in water.\n";
}
}
}
}//End nested while loop
if(count >= limit)
{
std::cout << "\nI'm sorry, but you are out of guesses.\n";
}
else
{
std::cout << "\n*** Good job! You guessed the correct animal! ***\n";
std::cout << "\t\t><)))º> ❤ <º)))><\t\t\n";
}
char choose = 'Y' ;
std::cout << "Would you like to try again?(y/n): ";
std::cin >> choose;
if(std::toupper(choose) == 'N') break;
} while(true);
return 0;
}

Error checking two separate inputs

I'm trying to check two separate inputs if they are integers or not. I'm able to error check one input but I'm not quite sure how to check two separate ones if I'm using the 'get' function and both inputs are from the 'cin' stream. Using c++.
My code for checking one integer is displayed below.
#include <iostream>
using namespace std;
int main() {
int input;
cout << "Enter an integer: ";
cin >> input;
char next;
int x=0;
int done = 0;
while (!done){
next = cin.get();
if (next == ' ' || next == '\n'){
cout << "The Integer that you have entered is: " << input << "\n";
done = 1;
}
else if (next == '.'){
cerr << "Error: Invalid Input. Not an Integer." << "\n";
done = 1;
}
else{
cerr << "Error: Invalid Input. Not a number." << "\n";
done = 1;
}
}
return 0;
}
Well you could use >> into an int all the way through, drop all that get() stuff and character handling, and check cin.fail(). For example (I'll leave working this into your program and repeating it in a loop as an exercise for you):
int x;
cin >> x;
if (cin.fail())
cout << "Not a valid integer." << endl;
You can handle all subsequent input in exactly the same way. There's no reason to only limit operator >> to the first input.

Checking for Char/Int

Alright so I am doing an assignment and am getting frustrated. The assignment wants me to ask the user for a number then say if the number is even or odd, but if the user types "done" the program will exit.
So my question is how do you check the input for character/int at the same time, then decide which it is.
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
bool isEven(int userAnswer);
using namespace std;
int userAnswer;
int main()
{
cout << "Which number would you like to check?" << endl;
cin >> userAnswer;
if (isEven(userAnswer) == false)
{
cout << userAnswer << " is a odd number." << endl;
}
else if (isEven(userAnswer) == true)
{
cout << userAnswer << " is a even number." << endl;
}
cin.get();
cin.get();
return 0;
}
bool isEven(int userAnswer)
{
if (userAnswer % 2 == 0)
{
return true;
}
else
{
return false;
}
}
Read into a string (which works in both cases), then parse the string yourself.
Read into a std::string and exit if done is in the string. Otherwise convert to int and carry on as you are. Hint: see std::stoi