Question and Answer Section 1
Having received a number of emails regarding doubts in C++, I thought that it would be useful to start up a new section on Question and Answers. So, let's get on with it :
Question :
Hi SS: Thanks for your concern and prompt reply. The section on "beware of character arrays (and the compiler in general)" helped clarify some problems I've been having with how "cin" and "cout" work, although I'm still having some difficulties with other variables like int, double and char. I tried out the following source code to understand how "cin" reads in data from standard inputs and what happens when the types of data do not match:
#include <iostream.h>
int main (void)
{
int i;
double d;
char c;
char str[100]
cout << "please input: " << endl;
cin >> i >> d >> c >> str;
cout << " i = " << i << endl;
cout << "d = " << d << endl;
cout << " c = " << c << endl;
cout << " str = " << str << endl;
return 0;
}
The following are some of the inputs I tried out with their corresponding outputs:
Input: 30 40.0 Pearl Harbor
Output: 30 40 P earl
input: 30.0 40.0 Pearl Harbor
Output: 30 0 4 0.0
Input: 30 40 Pearl Harbor
Output: 30 40 P earl
Input: Pearl 20 Harbor 30.0
Output: -858993460 -9.25596e+061 (strange symbol) =
Input: 20 Pearl Harbor 30.0
Output: 20 -9.25596e+061 (the strange symbol again) =
Answer :
The main point to remember is that in your coding you have written :
cin >> i >> d >> c >> str;
So the compiler expects to get inputs in the same order (integer, then double, then character and then a string).
When you mix up the inputs, you confuse the compiler. I'll explain it below.
First of all, an integer will not accept decimal point.
A 'double' will accept the decimal point.
A character means any one character (alphabet or a number). If you type the input for a character as :
'pearl harbor' then the compiler will take in only the first letter
(p).
A string (or array) of characters will accept many characters except a space (space is not a character). So if you give the input to an array of characters as : 'pearl harbor' then the compiler will take the input
upto the space. Thus, the variable will store only the word 'pearl'.
You've made use of 4 input variables. And you're getting the values for all of them at the same time. So, the compiler will distinguish between the variables by use of the space.
For example if I give the input as :
20 30.2 pearl harbour
then from your code (cin >> i >> d >> c >> str;) the compiler will take the first number as 'i'. The compiler reads till it encounters a space. 20 is an integer and
'i' is also an integer so
20 is stored in 'i'.
After the space, the compiler reads whatever is typed as the value for the second variable
'd'. Again it stops reading when it encounters a space. 'd' has been declared as a double and it can store a decimal point. So
30.2 is stored in 'd'.
The next input is 'c', which is a character. So the compiler can accept only one letter and hence it will store
'p' in 'c'.
The last input is a character array 'str'. The compiler reads
'earl' (since it has read 'p' in the previous input). Then it encounters a space and it stops reading. It assign
'earl' to the variable 'str'.
Suppose your input is given as follows : Pearl 20 Harbor 30.0
then the compiler gets into a lot of confusion. The reason is simple. The first variable was declared as an integer and it expects to read an integer. But what it reads is not an integer. SO it gets confused and stores some weird value in the variable. Now because the first input is something strange, all your remaining 3 variable values are also weird.
If your input is : 20 Pearl Harbor 30.0
then the compiler has no problem for the first variable (since 20 is an integer). But the next variable is a double.
The compiler reads something else and so gets confused. Hence your last 3 variable values will be weird. The first one only will be correct and so your output is :
Output: 20 -9.25596e+061 (the strange symbol again) =
Some of you might be interested to know how to get the space as an input (you
might not want the compiler to stop reading when it encounters a space). You
need to use the gets ( ) function. Check
out : Useful functions for details.
Check out the next Q
and A page 2
Email me at : ssbell2000@yahoo.com
Or Go back to the Contents Page
Copyright © 2002 Sethu Subramanian All rights reserved.