Hi all and thanks in advance for any help.

I have a basic program that needs to read a number of inputs from a user.
To all these inputs I assign a default value before I call for the user to give diffrent input.
Here is the thing, the input is of type double. I use the folloing code for example:
double a=17.5;
double b=10;
double c=9;

std::cout<<"enter a Real value for a:"<<std::endl;

std::cout<<"endt a Real value for b:<<std::endl;


If the user enters a char such as 'e', then the value of 'a' doesn't change, however, the programe ignores all the other std::cin, but it does print out all the output... hence the user doens't get a chance to input the rest of the data because of his first mistake.

How do I prevent this from happening?

Cheers for the help.

Recommended Answers

All 9 Replies

couldn't edit, here is a program and it's resaults for example:

void main()
double x=1;
double y=2;
double z=3;
cin >> x;

cout<<"resaults: x:"<<x<<",  y:"<<y<<",  z:"<<z<<endl;



resaults: x:1,  y:2,  z:3


I believe you can test if it actually worked ie:

if ( ! cin>> x )
cout<<" BAD VALUE FOR X "<<endl;

I can, but this doesn't solve the problum - The program still ignores the rest of the cin's without pausing for input.

for example, the following is an infinate loop on the input 'a':

while (!(cin >> x))
cout<<"bad input";

How about taking them in as strings and then converting them later to doubles?

It's a possability, but a dirty one... (one that I am trying to avoide)
I guess a better question would be, why the program is ignoring all the other cin's



The user of a program may be one of the reasons that provoke errors even in the simplest programs that use cin (like the one we have just seen). Since if you request an integer value and the user introduces a name (which is a string of characters), the result may cause your program to misoperate since it is not what we were expecting from the user. So when you use the data input provided by cin you will have to trust that the user of your program will be totally cooperative and that he will not introduce his name when an interger value is requested. Farther ahead, when we will see how to use strings of characters we will see possible solutions for the errors that can be caused by this type of user input.

That's no help, my program is suppose to be 'stupid user safe'.

I did slove the problem like this (reminder, x is a double variable):

if (!(cin>>x)){
	cout<<"bad input";

This way, the following cin's are not ignored.
However, I don't understand why I need the line cin.get(); ? why doesn't the clear() method reset the stream? Why is the following an endless loop on an input 'a'

while (!(cin>>x)){
	cout<<"bad input";

Probably because hitting 'Enter/Return' on your keyboad is considered a character.

You can also after cin.clear() do this. . .

std::string badchars;
std::cin >> badchars;
std::cout << "\"" << badchars << "\"" << " is not a character. Please try again.";

put it in a loop and you can have the user try again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.