A very very small problem
Moderator: Board moderators
A very very small problem
What is the syntax " while( scanf("%d %d %d",&i,&j,&k)==3 ) ......"
in C++ (using cin) ?
in C++ (using cin) ?
Code: Select all
( cin>>i>>j>>k )
Does it return "EOF" ?
-
- New poster
- Posts: 35
- Joined: Sat Jan 05, 2002 2:00 am
- Contact:
As I know, operator >> returns a stream. That is why one writes cin>>i>>j>>k which is nothing but ((cin>>i)>>j)>>k If input fails, the returned stream is converted to falsehank wrote:What it may return when reaching the end of the file ?Code: Select all
( cin>>i>>j>>k )
Does it return "EOF" ?
-
- Learning poster
- Posts: 57
- Joined: Sun Sep 29, 2002 12:00 pm
- Location: in front of the monitor :-)
- Contact:
afaik...
Code: Select all
while( x ) {...} // works while x is non-zero
// the following works because cin returns a stream when something is read
while( cin >> x) { ... }
// however cin returns null when no data can be read, so the loop breaks
Re: A very very small problem
Havn't see the equivalent code, thoughhank wrote:What is the syntax " while( scanf("%d %d %d",&i,&j,&k)==3 ) ......"
in C++ (using cin) ?
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
The solution was mentioned somewhere above, but rejected by someone as incorrect.
The following code:
[cpp]if(cin>>k>>l>>m){
/* there are three or more integers in the input */
}else{
/* there are two or less integers in the input */
}[/cpp]
is correct, accepted by standard and will not fail.
For instance:
[cpp]#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int k,l,m;
#ifndef __USE_IOSTREAM
printf("stdio version\n\n");
if(scanf("%d %d %d",&k,&l,&l) == 3){
printf("ok\n");
}
#else
cout<<"iostream version\n\n";
if(cin>>k>>l>>m)cout<<"ok\n";
#endif
}[/cpp]
The following code:
[cpp]if(cin>>k>>l>>m){
/* there are three or more integers in the input */
}else{
/* there are two or less integers in the input */
}[/cpp]
is correct, accepted by standard and will not fail.
For instance:
Code: Select all
bash-2.05b$ cat t.cpp
#include <cstdio>
using namespace std;
int main(){
int k,l,m;
#ifndef __USE_IOSTREAM
printf("stdio version\n\n");
if(scanf("%d %d %d",&k,&l,&l) == 3){
printf("ok\n");
}
#else
cout<<"iostream version\n\n";
if(cin>>k>>l>>m)cout<<"ok\n";
#endif
}[/cpp]
Code: Select all
bash-2.05b$ g++ t.cpp -o t.exe
bash-2.05b$ echo 2 3|./t
stdio version
bash-2.05b$ echo 2 3 4|./t
stdio version
ok
bash-2.05b$ echo 2 3 4 5|./t
stdio version
ok
bash-2.05b$ g++ -D __USE_IOSTREAM t.cpp -o t.exe
bash-2.05b$ echo 2 3|./t
iostream version
bash-2.05b$ echo 2 3 4|./t
iostream version
ok
bash-2.05b$ echo 2 3 4 5|./t
iostream version
ok
Because the typecast-to-int-operator is defined for iostreams to be 0 if we've read past the end of file, and 1 otherwise. And when you put an iostream in a while-condition, you get an implicit typecast to int, i.e. you actually say "while((int)(cin >> x))..."hank wrote:[cpp]
while( x ){ // it works when x is true
...
}
//but why it can also work if cin doesn't return a boolean value?
while( cin>>x ){
...
}
[/cpp]