0

I have a while loop that scans in an input set of integers. It scans and prints all of the numbers with "..." at the end and skips to the next line. However, the script does not: execute past the while loop and print TEST.

For example, I enter: 3 44 62 1

It prints: 3...

44...

62...

1...

When it should print: 3...

44...

62...

1...

TEST

while(scanf("%d", &n) != -1) {
    x[i] = n;
    i++;

    printf("%d", n);
    printf("...\n");
}

printf("TEST");

What am I doing wrong?

5
  • 1
    What is -1? Where did -1 come from? Why do you expect your scanf to return -1 at the end of the input? In other words, why do you expect this code to work the way you want it to? Commented Nov 25, 2012 at 7:23
  • -1 is returned by scanf() to signify the end-of-characters. It stops reading integers at that point. Commented Nov 25, 2012 at 7:25
  • 4
    Firstly, there's no such thing as "end-of-characters"? Secondly, fscanf returns EOF at the end-of-file. Not -1, but EOF. Thirdly, scanf reads from standard input, which never ends when it is reading from keyboard. You can "simulate" end-of-file condition in standard keyboard input by system-dependent means (like Ctrl+D or Ctrl+Z). But without it, just because you stopped typing it does not mean that your input ended. scanf will simply wait forever for more input, which is exactly what it is doing in your case. Commented Nov 25, 2012 at 7:26
  • many times -1 is returned at EOF, but not always. As far as I know in C the actual specification is just that it return a number less than 0 Commented Nov 25, 2012 at 7:28
  • 2
    As I remember it, EOF is only required to be unequal to any other character value. Regardless, writing -1 when you mean EOF is very bad style. Commented Nov 25, 2012 at 7:33

1 Answer 1

6
  1. scanf("%d", &n) != 1 is wrong. It should be scanf("%d", &n) == 1.
  2. You're expecting the loop to end just because you hit enter? As written, your program will only stop if scanf fails to read a number due to reaching the end of the input file. If you're on Unix, you can signal EOF from the terminal by hitting Ctrl-D. If you're on Windows, it's Ctrl-Z Enter. (Also, don't rely on EOF being -1; it's not portable.)
Sign up to request clarification or add additional context in comments.

5 Comments

Changing it to scanf("%d", &n) == 1 makes no difference. Hitting Ctrl-D does not help.
@JoshuaShor: The changes proposed should fix your problem. Which platform are you on? In case of emergency, type help or quit; neither of those is a number, so scanf() will report 0 values returned, breaking the loop. However, if you type Control-D when you're at the beginning of a line (after a newline, and assuming that you've not set your terminal's EOF setting to some other value), then the system will return 0 characters to the read() system call, which will be interpreted as EOF.
If you're not at the beginning of a line, you can also hit Ctrl-D twice (which works for interesting reasons).
Interestingly enough, hitting Ctrl-D doesn't work; hitting it multiple times until I hit "TEST" does.
Take a look at stty -a output. You should see something like eof = ^D somewhere in it; if you see a different character, then you've got an obvious problem. There's always a chance that SSH is confusing things, though it is pretty unlikely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.