# when I enter the value of A to be, say, "b", I get 'weird' output

This is a discussion on when I enter the value of A to be, say, "b", I get 'weird' output within the C++ Programming forums, part of the General Programming Boards category; Hi When I enter the value of A to be, say, "b", the output given below is what I get. ...

1. ## when I enter the value of A to be, say, "b", I get 'weird' output

Hi

When I enter the value of A to be, say, "b", the output given below is what I get. But I was expecting it to return "Input error" because this is what I mentioned in the "else" statement. Where am I going wrong? Please guide me on this and please don't forget I'm newbie to this programming world! Thanks a lot.

OUTPUT:
Code:
enter the numbers A, B, C
enter A = b
enter B = enter C = the ascending order is 2.8026e-045 9.52883e-044 3.21412e-039

Press any key to continue . . .

Code:
/* arranging the three numbers in ascending order assuming the numbers
are distinct */

// there would be six permutations in total

#include <iostream>
#include <cstdlib>

using namespace std;

int main()

{
float A, B, C;

cout << "enter the numbers A, B, C" << endl ;

cout << "enter A = " ;
cin >> A;

cout << "enter B = " ;
cin >> B;

cout << "enter C = " ;
cin >> C;

{
if ( (A < B) && (B < C) )
cout << "the ascending order is " << A << '|' << B << '|' << C << endl;

else if ( (A < C) && (C < B) )
cout << "the ascending order is " << A << '|' << C << '|' << B << endl;

else if ( (B < A) && (A < C) )
cout << "the ascending order is " << B << '|' << A << '|' << C << endl;

else if ( (B < C) && (C < A) )
cout << "the ascending order is " << B << '|' << C << '|' << A << endl;

else if ( (C < A) && (A < B) )
cout << "the ascending order is " << C << '|' << A << '|' << B << endl;

else if ( (C < B) && (B < A) )
cout << "the ascending order is " << C << '|' << B << '|' << A
<< endl;

else
cout << "Input error" << endl;

}

system("pause");

}

2. You have to test cin directly, if you want to detect input errors.

Like
Code:
if ( cin >> A >> B >> C ) {
} else {
// user didn't do it right
}

3. Because when you tried to enter the alpha character cin failed, thus leaving the entry in the input buffer and not changing the value of your variable.

Jim

4. Originally Posted by jimblumberg
Because when you tried to enter the alpha character cin failed, thus leaving the entry in the input buffer and not changing the value of your variable.

Jim
Thanks a lot, Salem, Jim.

Jim, would you please explain it a bit more? Is this because of declaration of A, B, and C as floats? So, when I enter A as a non-float, the weird behaviour occurs before I get a chance to enter the values of B and C. In other words, the cin wouldn't let anything else to enter the variable space except a float. Please guide me on this. Thanks, and don't forget I'm a beginner.

5. When you try to insert the character into the numeric variable with cin, cin will fail. The cin stream knows what type of variables you are trying to insert your value into, it knows you can not put a character into a numeric variable so it stops processing and set the error flag. When it stopped processing the variable that caused the error and any variable after that is skipped until you, the programmer clear cin's error state.

To illustrate:
Code:
#include <iostream>
using namespace std;

int main()
{
int mvalue = -230;
int newValue = 3000;
cout << "Please enter a character value: ";
cin >> mvalue;
cin >> newValue;

cout << "The value of mvalue is: " << mvalue << " The value of newValue is: " << newValue << endl;

if(!cin) // Check if cin is in the fail mode:
{
char charValue;
cin.clear();  // Clear the error flags.
cin >> charValue;

cout << "The character you entered is: " << charValue << endl;
}
return 0;
}
If you enter, compile and run the above program and enter a character when prompted you should see that the second input is skipped and the values of both variables did not change. And after clearing the error state you can retrieve the character that caused the problem. Normally you would just retrieve and dispose of the problem characters with:

Code:
if(!cin) // Check if cin is in the fail mode:
{
cin.clear();  // Clear the error flags.
cin.ignore(200,'\n');
}
The cin.ignore() will try to retrieve up to 200 characters or the newline character which ever occurs first.

Also note if you try to enter 2 successive integer values but your first entry is a floating point number (2.02) the second insertion will fail. Because the first entry will extract the integer portion of the floating point number leaving the fractional part in the input stream (.02). Which since the next item is not a valid integer (the decimal point) this insertion will fail.

Jim

6. Originally Posted by jimblumberg
When you try to insert the character into the numeric variable with cin, cin will fail. The cin stream knows what type of variables you are trying to insert your value into, it knows you can not put a character into a numeric variable so it stops processing and set the error flag. When it stopped processing the variable that caused the error and any variable after that is skipped until you, the programmer clear cin's error state.

To illustrate:
Code:
#include <iostream>
using namespace std;

int main()
{
int mvalue = -230;
int newValue = 3000;
cout << "Please enter a character value: ";
cin >> mvalue;
cin >> newValue;

cout << "The value of mvalue is: " << mvalue << " The value of newValue is: " << newValue << endl;

if(!cin) // Check if cin is in the fail mode:
{
char charValue;
cin.clear();  // Clear the error flags.
cin >> charValue;

cout << "The character you entered is: " << charValue << endl;
}
return 0;
}
If you enter, compile and run the above program and enter a character when prompted you should see that the second input is skipped and the values of both variables did not change. And after clearing the error state you can retrieve the character that caused the problem. Normally you would just retrieve and dispose of the problem characters with:

Code:
if(!cin) // Check if cin is in the fail mode:
{
cin.clear();  // Clear the error flags.
cin.ignore(200,'\n');
}
The cin.ignore() will try to retrieve up to 200 characters or the newline character which ever occurs first.

Also note if you try to enter 2 successive integer values but your first entry is a floating point number (2.02) the second insertion will fail. Because the first entry will extract the integer portion of the floating point number leaving the fractional part in the input stream (.02). Which since the next item is not a valid integer (the decimal point) this insertion will fail.

Jim
Thanks a lot, Jim. You are so nice and I have learned much from you.

I was just wondering how C++ works internally. Once I have tried to enter a character into a numeric variable, then the cin would proceed to error state and won't ask for further inputs for other variables if there are any. The "if(!cin)" statement is at end. That error point lies between that "if" statement and where I entered a wrong value. So, how would it (C++, or compiler?) know that before it proceed to set error flag it should go a step further to see if there is an "if(!cin)" condition to clear the error flag? Is this one of the features of C++ that in the background it goes through all the code to see if the programmer has set some criterion/method to encounter error(s)? I hope you understand my question. If there is something unclear please do let me know.

The cin.ignore() will try to retrieve up to 200 characters or the newline character which ever occurs first.
Does this mean that if I entered any of those 200 characters or the newline character (\n) it would be able to tell me which character I entered in the cout?

Because the first entry will extract the integer portion of the floating point number leaving the fractional part in the input stream (.02).
I didn't believe it at first sight! Then, I used your own code to test it for my own satisfaction. As you have declared mvalue newValue variables to be int, I tried to enter a decimal value for mvalue. It extracted integer part from the decimal value of mvalue I entered and then didn't care to ask me to enter the value of newValue. It displayed the extracted integer value for mvalue and initially set value newValue.

Now please help me with the above queries. I thank you for your guidance and time you invest in helping others like me.

Best wishes
Jackson

7. So, how would it (C++, or compiler?) know that before it proceed to set error flag it should go a step further to see if there is an "if(!cin)" condition to clear the error flag?
You the programmer are the one responsible for validating user input. C++ does not go a step further than set the error flag, it is up to you to check the flags and do something about them.
Does this mean that if I entered any of those 200 characters or the newline character (\n) it would be able to tell me which character I entered in the cout?
The cin.ignore() function just discards the items left in the buffer. It does not save or report anything.

Jim