Dealing with bad input

Hello, I'm a C++ beginner and I have a doubt about bad input, why in this program if I enter the value 2.5 and 3.5 the reading of 3.5 fails ? If I write this program using just one variable it doesn't happen, why ? I have the same problem if I write the same program but using doubles (if I enter two ints, the second reading fails) why ?


1
2
3
4
5
6
7

	int a = 10;
	int b = 100; 
	cin >> a >> b;
	cout << a << " " << b << "\n"; 

Don't read directly from standard input if you don't want to deal with its error state.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    int a, b;
    std::cout << "Enter two integers on separate lines." << std::endl;
    std::string line;
    while(std::getline(std::cin, line) && !(std::istringstream(line) >> a))
    {
        std::cout << "Please re-enter the first integer." << std::endl;
    }
    while(std::getline(std::cin, line) && !(std::istringstream(line) >> b))
    {
        std::cout << "Please re-enter the second integer." << std::endl;
    }
    std::cout << "You entered " << a << " and " << b << "." << std::endl;
}
(untested, and doesn't handle early end of input)
Last edited on
One feature with cin is that it expects your entry to conform to the variable type you have declared. In the case of int a, a decimal point is undefined in type int, so cin will begin a chase, in which it only ends when you interrupt the program.

I'm not sure about the doubles situation. On my system as long as I input a number it does what it's supposed to do. In this:
1
2
3
4
	int a = 10;
	int b = 100; 
	cin >> a >> b;
	cout << a << " " << b << "\n";

If I enter 3.5, I get:
3   100
3   100
3   100
3   100
3   100
3   100
3   100
3   100
3   100
3   100
3   100
3   100


If I do this:
1
2
3
4
	double a = 10;
	double b = 100; 
	cin >> a >> b;
	cout << a << " " << b << "\n"; 
You can't enter a non-int value when you've declared an int, or cin >> will go into an infinite loop. I don't know enough about the cin command, so I can't say why.

I get the same result whether I put in one variable int a, or two int a, int b. Also have no problems with declaring them as doubles.
PCrumley48 wrote:
You can't enter a non-int value when you've declared an int, or cin >> will go into an infinite loop. I don't know enough about the cin command, so I can't say why.
It does not go into an infinite loop. It is put into an error state. While a stream is in an error state, all read/write operations do nothing. Either clear the error state (which is a nuisance) or don't allow the stream to enter the error state in the first place (as in my example).
Last edited on
Gotcha. It looks infinite on the monitor. . . ! But right usage is right usage, after all. Like I said, I don't know enough about cin, maybe should read up on it more.
std::cin is a std::istream that just happens to take input from standard input. You should research how the C++ I/O Streams library works.
Topic archived. No new replies allowed.