1 / 7
Oct 2009

i know it may sound a bit stupid, but i'm stuck at the MERGSORT problem because i don't know how to read the input...

i have to read whitespace separated integers until the end of the line, but i don't seem to be able to do that..

can anyone help me?

  • created

    Oct '09
  • last reply

    Oct '09
  • 6

    replies

  • 4.3k

    views

  • 2

    users

  • 1

    link

could you explain to me? i really don't know how to use only scanf to do that, as the problem doesn't specify the quantity of numbers.. thanks

That page specifies what scanf returns when reaches the end of file. So just read numbers in inside a while(true) loop, and break when scanf reaches the end of file.

that didn't work...

i = 0;
while(true){
      if(scanf("  %d", &v[i++]) == EOF) break;
}

the numbers keep on being read even after pressing enter...

Well, pressing enter doesn't tell the program it has reached the end of the file - how does it know you aren't going to type in more numbers on the next line?

Either redirect the input from a file on the command line, which is probably a better idea anyway, or use ctrl-z to signify EOF when entering input.