A very very small problem

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.

A very very small problem

Post by hank »

What is the syntax " while( scanf("%d %d %d",&i,&j,&k)==3 ) ......"
in C++ (using cin) ?

Adil
Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:

Post by Adil »

hi. i think you can use:[cpp] while(cin >> i >> j >> k) ...[/cpp]

hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.

Post by hank »

Code: Select all

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

abiczo
New poster
Posts: 7
Joined: Sat Apr 06, 2002 2:00 am
Location: Hungary

Post 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.

Alexander Denisjuk
New poster
Posts: 35
Joined: Sat Jan 05, 2002 2:00 am
Contact:

Post 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

hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.

Post by hank »

Is it correct?

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

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

ec3_limz
Learning poster
Posts: 79
Joined: Thu May 23, 2002 3:30 pm
Location: Singapore

Post by ec3_limz »

Is it correct?
The following will work though.

Code: Select all

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

anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post 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.
"Everything should be made simple, but not always simpler"

hank
Experienced poster
Posts: 146
Joined: Mon Feb 04, 2002 2:00 am
Location: VCORE.

Post 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]

Adil
Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:

Post 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

beeplove
New poster
Posts: 17
Joined: Tue Sep 30, 2003 9:28 pm
Location: Boston, MA, USA
Contact:

Re: A very very small problem

Post 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

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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

Per
A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden

Post 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))..."

Post Reply

Return to “C++”