Topic: Question about cin
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 14 Feb 1995 18:30:25 GMT Raw View
In article A9A@nntpa.cb.att.com, sjh@hogpc.ho.att.com (-S.HORVATH) writes:
>Ok, I'm new at this, so maybe this is dumb but...here goes.
>Assume the variables are declared, and it compiles.
>In the first while if I enter a ^D, will it put
>the ^D into choice?
>
>
> while (cout << "Enter int: " && cin >> v)
> a[++N] = v;
> //fine, it loads the array and exits on a type mismatch or ^D
>
> while (cout << "\nChoice: A, B : " && cin >> choice)
> switch (choice)
> {
> case 'A':
> //....etc
> };
>
>Then it prints out Choice: A, B and skips the switch altogether ???
First of all, ^D is special on some systems, not on others. Let's
assume that on your system it acts like end-of-file when typed from
a terminal. That would cause input to fail at the point where
the EOF condition was found.
If attempted input fails, the target variable is not modified. It
has whatever value it had before the input was attempted. Example:
char c = 'x';
cin >> c;
cout << c << endl;
If the input fails, the next statement will print 'x', since that was
the former value of c. If c was not initialized, there is no telling
what will be printed if input fails.
---
Steve Clamage, stephen.clamage@eng.sun.com