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?
-1? Where did-1come from? Why do you expect yourscanfto return-1at the end of the input? In other words, why do you expect this code to work the way you want it to?fscanfreturnsEOFat the end-of-file. Not-1, butEOF. Thirdly,scanfreads 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.scanfwill simply wait forever for more input, which is exactly what it is doing in your case.-1when you meanEOFis very bad style.