Page 1 of 1

A very very small problem

Posted: Fri May 16, 2003 7:11 am
by hank
What is the syntax " while( scanf("%d %d %d",&i,&j,&k)==3 ) ......"
in C++ (using cin) ?

Posted: Fri May 16, 2003 12:18 pm
by Adil
hi. i think you can use:[cpp] while(cin >> i >> j >> k) ...[/cpp]

Posted: Thu May 22, 2003 4:06 pm
by hank

Code: Select all

( cin>>i>>j>>k )
What it may return when reaching the end of the file ?
Does it return "EOF" ?

Posted: Thu May 22, 2003 5:29 pm
by abiczo
As far as I know cin converts to false if EOF is reached. So you can use
[cpp]int k;
while( cin >> k ) {
process( k );
}[/cpp]
to read numbers until EOF.

Posted: Fri May 23, 2003 9:36 am
by Alexander Denisjuk
hank wrote:

Code: Select all

( cin>>i>>j>>k )
What it may return when reaching the end of the file ?
Does it return "EOF" ?
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 false

Posted: Sun May 25, 2003 1:47 am
by hank
Is it correct?

[cpp]if( (cin>>i>>j>>k)==true ){

........
}[/cpp]

Posted: Mon Jun 09, 2003 3:23 pm
by ec3_limz
Is it correct?
The following will work though.

Code: Select all

if (cin>>i>>j>>k) {
...
}

Posted: Wed Jun 18, 2003 4:54 pm
by anupam
i don't think so,
if((cin>>i>>j)==true) is not correct coz,
cin doesn't return boolean value.
sometimes it may work but most of the times it will fail.
so better not to use it.

Posted: Fri Jul 04, 2003 4:40 pm
by hank
[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]

Posted: Fri Jul 04, 2003 7:18 pm
by Adil
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

Posted: Tue Mar 16, 2004 10:33 pm
by beeplove
hank wrote:What is the syntax " while( scanf("%d %d %d",&i,&j,&k)==3 ) ......"
in C++ (using cin) ?
Havn't see the equivalent code, though

Posted: Wed Mar 17, 2004 5:48 pm
by Krzysztof Duleba
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:

Code: Select all

bash-2.05b$ cat t.cpp
[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]

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

Posted: Fri Mar 19, 2004 12:14 pm
by Per
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]
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))..."