Is There a problem?

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

Moderator: Board moderators

Post Reply
creeping
New poster
Posts: 3
Joined: Sun Jun 11, 2006 10:58 pm

Is There a problem?

Post by creeping »

Hi,
Problem states that input ends with a normal end of file marker. and i use something like

Code: Select all

char str[3001]
while(!cin.eof())
{
 cin.getline(str,3000);
.......
}
can it make a problem?
creeping
New poster
Posts: 3
Joined: Sun Jun 11, 2006 10:58 pm

Post by creeping »

it seems that there is a problem. when trying from keyboard,ctrl+z caused
it to take another blank line input. so i changed it as

Code: Select all

while(gets(str)!=NULL)
{
....
}
now,i got AC. but,exactly why? :-?
misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof »

IIRC, cin.eof() is only true after an end-of-input error occured.

The best practice is usually "read until no further read is possible", in your case one possibility would be

Code: Select all

string S;
while (getline(cin,S)) {
...
}
... or the gets() you used in your AC submission

One other thing, consider testing your programs with redirected I/O:

Code: Select all

my_program < input.txt > output.txt
this will save you the troubles you get in an interactive environment
creeping
New poster
Posts: 3
Joined: Sun Jun 11, 2006 10:58 pm

Post by creeping »

thank u very much
Post Reply

Return to “C++”