cin in while loop doesn't work properly (C++) - c++

This program should check if entered number is integer. It works fine with strings but not with doubles.
int test;
cout << "Enter the number:" << endl;
while(true) {
cin >> test;
if (!cin || test < 0) {
cout << "Wrong input, enter the number again:" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

test is int. istream >> operator is just dynamic casting to int and, then, you're losing decimal part.
Yo can just define test as float and cast it to int when needed.
Edit: Answering you last edit (I didn't refresh so I missed this part), what is happening is that, without the gotoyou're looping twice:
You enter 1.5
test is 1 and you don't enter if, so cin is not cleaned up.
loops again and cin immediately returns.
test is 0 so enters if statement and complains.
Hope this helps

Try this:
int test;
cout << "Enter the number:" << endl;
while ( true )
{
cin >> test;
if (!(test < 0 || !cin))
break;
}
cout << "Your chosen number is: " << test << endl;
Is that what you want?

Related

input validation in loop

I'm trying to make a validation loop in C++ that checks the user's input until they enter a number between 0 and 100 and however my loop only checks the first condition. Any guidance is appreciated!
#include <iostream>
using namespace std;
int main()
{
const int max_num = 100;
const int min_num = 0;
int num;
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
do {
if (!(cin >> num))
{
cout << "ERROR:The value provided was not a number" << endl;
cin.clear();
cin.ignore(1024, '\n');
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
}
else if (num<min_num || num>max_num)
{
cout << "ERROR: value out of range" << endl;
cin.clear();
cin.ignore(1024, '\n');
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
}
} while (!(cin >> num) || (num<min_num || num>max_num));
return 0;
}
Add lots of logging to your code so that you know what it's doing. This will help you find the problem. For example, instead of:
cout << "Enter a number between 0 and 100" << endl;
cin >> num;
Try:
cout << "Enter a number between 0 and 100" << endl;
cerr << "About to read into num outside the loop" << endl;
cin >> num;
cerr << "Read into num outside the loop, got: " << num << endl;
And so on, throughout your code. This should give you enough information to find the bug. Alternatively, use a debugger with a single step function to accomplish the same thing.
Check that in the part of while:
instead of
while (!(cin >> num) || (num<min_num || num>max_num));
this:
while (!cin || (num<min_num || num>max_num));
the same for the upper if
cin >> num means putting user input to the variable num . So you are trying to take user inputs 2 times in the loop. Maybe the check condition: (num == (int)num)will solve your problem. It will try to verify the number you have stored in num is really of the type int

If-Else Statement Returning Same Feedback

So for some reason no matter what I enter into this If-Else statement it returns with a "Program Aborted" as if I entered the wrong requested answers...very confused and I can't seem to find anything relevant enough around the site!
int ch;
cout << "Do you have any extra credit points? (Enter Y/y or N/n)" << endl;
cin >> ch;
int ExtraCredit;
if (ch == 'Y' || ch== 'y')
{
cout << "Enter the number of extra credit points: ";
cin >> ExtraCredit
}
else if (ch!='n' || ch!='N' || ch != 'y' || ch != 'Y')
{
ExtraCredit=0;
cout<< Invalid entry. Program Aborted." << endl;
return 0;
}
The issue appears very early:
int ch; // are you sure this should be an int?
cout << "Do you have any extra credit points? (Enter Y/y or N/n)" << endl;
cin >> ch;
cin behaves differently on int typed variables than on chars. When you enter "y" or "n" at the keyboard, cin will fail.
You can check whether cin failed by calling its fail() method, like so:
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if (std::cin.fail()) {
std::cout << ":(" << std::endl;
} else {
std::cout << "Your number was " << num << std::endl;
}
ints and chars are separate types, even though they can be compared based on the ASCII value of the char in C++. Because they are defined as separate types, when your code gets to the line cin >> ch and ch is of type int, it waits for something to be entered. The prompt tells the user to enter a char, and they do. The code sees the char, and as it wasn't an int, nothing is read in and the value of ch is correctly determined by your code to be not y, Y, n, or N. If you'd like to cin a char, declare char ch;. If you'd like to have an int, prompt the user to enter a number.

C++ Program glitch?

I need help debugging my code. So I made a program that adds and subtracts numbers but when I implemented a do-while loop to replay the program, the actual program closes and does not perform the do-while loop and does not replay the program. Is their something wrong with my code?
P.S. I am also using codeblocks IDE
#include <iostream>
using namespace std;
int main()
{
// Addition and Subtraction Calculator
int a_number, number1, number2, sum, number3, number4, subsum, again;
// subsum = subtracted sum
// number1 and number2 are variables that hold the users input for addition
// number3 and number4 are variables that hold the users input for subtraction
do
{
cout << "Addition & Subtraction Calculator" << endl;
cout << "-------------------------------------------" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "Please enter a number [1 or 2]" << endl;
cin >> a_number;
while (a_number < 1 || a_number > 2)
{
cout << "Please enter either 1 or 2" << endl;
cin >> a_number;
}
switch (a_number)
{
case 1:
cout << "Please enter a number" << endl;
cin >> number1;
cout << "Please enter another number" << endl;
cin >> number2;
sum = (number1 + number2);
cout << "The sum is " << sum << endl;
break;
case 2:
cout << "Please enter a number" << endl;
cin >> number3;
cout << "Please enter another number" << endl;
cin >> number4;
subsum = (number3 - number4);
cout << "The answer to the subtraction problem is: " << subsum << endl;
break;
}
cout << "Do you want to try again? [y/n]" << endl;
cin >> again;
}
while (again == 'y' || again == 'n');
return 0;
}
OK. So the OP is using an int where they should have used a char. That covers the immediate problem. int again should be char again.
But there is an important point the other answers have missed.
int again;
cin >> again;
The user input will be converted into an integer as required by again. Inputting y or n fails to convert to an integer as neither y nor n are numbers and cannot be converted. again remains unchanged, keeping whatever junk value happened to be sitting at that spot in memory and might actually be a y or an n, but more importantly cin is now in an error state that needs to be cleared before continuing.
cin would have notified the OP of this if it had been tested. So let's test it.
int again;
if (cin >> again)
{
// got good input. Do something with it.
}
else
{
// got bad input.
cin.clear();
// that bad input is still in the buffer and needs to be removed
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// the above line will wipe out everything to the end of the stream or
// end of line, whichever comes first.
}
Why this is important: Because the OP is doing a lot of numeric input with cin and none of it is checked for validity. For example:
cout << "Please enter a number [1 or 2]" << endl;
cin >> a_number;
The program is broken completely and cannot exit without a kill signal if the user types in anything but a number.
Always check the error state and return codes. They are there to help. Always validate user input before using it. Users are evil and will try to break your program. Don't let them.
use char again; instead of int again;
in your code again is int and when in (again == 'y' || again == 'n')you compare again (an int) with a char, that does not make sense
You need to change the again variable to a char datatype because you need to store text. Something like this:
char again;
You also need to change the while statement to:
while(again != "n");
Or
while(again == "y");

Why is my If/Else statment block not executing completely?

I'm doing a simple project for my CS class. The goal is to have a person input the amount of each fruit (apples, bananas, oranges) they are purchasing, and the program calculates the total and presents an invoice at the end. My Professor wants us to also include an input check, to verify that the input is a number between 0 and 100. to do this, I have this section of code.
string name;
int apples, oranges, bananas;
int FRUIT_MAX = 100;
int FRUIT_MIN = 0;
float appleCost, orangeCost, bananaCost,
subTotal, tax, total;
cout << "Welcome to Bob's Fruits, what is your name..." << endl;
getline(cin, name);
cout << "How many apples would you like" << endl;
cin >> apples;
cout << endl;
//checking if user entered a number for apples
if (apples >= FRUIT_MIN && apples <= FRUIT_MAX)
{
cout << "Thanks" << endl;
}
else //makes the user retype entry if invalid
{
cout << "Please input a number thats 0 or greater than 0. " << endl;
cin >> apples;
cout << endl;
}
cout << "How many oranges would you like" << endl;
cin >> oranges;
if (oranges >= FRUIT_MIN && oranges <= FRUIT_MAX) //checking to see if number is good
cout << "Thanks" << endl;
else //makes the user retype entry if invalid
{
cout << "Please input a number thats 0 or greater than 0." << endl;
cin >> oranges;
cout << endl;
}
cout << "How many bananas would you like" << endl;
cin >> bananas;
if (bananas >= FRUIT_MIN && bananas <= FRUIT_MAX)
cout << "Thanks";
else
{
cout << "Please input a number thats 0 or greater than 0.";
cin >> bananas;
cout << endl;
}
When I enter a value between 0-100, I receive the proper "thanks" output and then it moves on to the next question. When I enter a number outside of 0-100, the else statement triggers sucsessfully, and the program asks for a number between 0-11.
The problem is when a letter is input. If a letter is input, the program skips through every remaining line, ignoring any additional cin commands, and displays the formatted invoice with all negative numbers. Any ideas why this is happening?
When cin gets an invalid value, it sets a failbit.
int n;
cin >> n;
if(!cin)
{
//not a number, input again!
}
You need to use cin.ignore() so that the input will be 'reset' and request the input again.
you can change the cin part into
while (!(cin>>apples)) {
cout<<"Type Error"<<endl;
cin.clear();
cin.sync();
}
The Problem ist that you do not check for proper type of your input.
Your apples variable is an Int. So as long as a user enters an Int everything will be fine.
But what happens if he or she enters a Char ?
The answer is as one of the guys before me mentioned is that the cin operation will fail.
What can you do to prevent or better to say handle this situation:
#include<iostream>
using namespace std;
int main(int argc , char** argv) {
int apples = 0; //Its always good to initialise a var with a value
cout << "Please enter a number: " << endl;
cin >> apples;
if(!cin) {
cout << "Not a number!" << endl;
// Handle the error
}
else {
cout << "A number was entered" << endl;
}
return 0;
}
Instead of checking for !cin you can also use cin.fail() which will be true if the last cin operation failed.
If you want to read more about cin or inputstreams in generell I would advise you to take a look at the C++ reference.

specifying a cin value (c++)

say I have:
int lol;
cout << "enter a number(int): ";
cin >> lol
cout << lol;
If I type 5 then it'll cout 5. If I type fd it couts some numbers.
How can I specify the value, like say I only want it an int?
If you type in fd it will output some numbers because those numbers are what lol happens to have in them before it gets assigned to. The cin >> lol doesn't write to lol because it has no acceptable input to put in it, so it just leaves it alone and the value is whatever it was before the call. Then you output it (which is UB).
If you want to make sure that the user entered something acceptable, you can wrap the >> in an if:
if (!(cin >> lol)) {
cout << "You entered some stupid input" << endl;
}
Also you might want to assign to lol before reading it in so that if the read fails, it still has some acceptable value (and is not UB to use):
int lol = -1; // -1 for example
If, for example, you want to loop until the user gives you some valid input, you can do
int lol = 0;
cout << "enter a number(int): ";
while (!(cin >> lol)) {
cout << "You entered invalid input." << endl << "enter a number(int): ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
// the above will loop until the user entered an integer
// and when this point is reached, lol will be the input number