0

SO in linux i could probably end a while-loop with this kind of code if I compile with gcc:

#include <stdio.h>

int main()
{   
    int s;
    while(scanf("d%",&s)!=EOF);
    {   
        scanf("%d",&s);
          }
    return 0;
}

However this does not work with a windows computer and the compiler Im using is Microsoft Visual Studio 12. Any suggestions?

Jask
  • 660
  • 10
  • 23
Tensora
  • 53
  • 8

3 Answers3

2
#include <stdio.h>

int main() {
    int s;
    while((s = getchar()) != EOF) {
        printf("%d\n", s);
    }
    printf("%d - at EOF\n", s);
}

you can try this

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
  • Just a question on this, if I want to print something in the whileloop before the user gets to input data, how could I do then? because now it prompts the user the first thing it does in the program? – Tensora Feb 16 '14 at 20:29
1

Try it with strg + c or strg + z. I think that was which overgives the EOF

user3202845
  • 41
  • 1
  • 7
  • does does not work either for me, atleast not for this code : ( – Tensora Feb 15 '14 at 16:39
  • 1
    ^Z *followed by Return* in a DOS window is equivalent to ^D in a Unixy tty. – zwol Feb 15 '14 at 16:41
  • Strangely, if I create a loop with while(1>0) , Ctrl+c ends the program, do you Think my teacher will be pleased with that? :p – Tensora Feb 15 '14 at 16:55
  • @zwol: MS-DOS follows the CP/M convention of treating control-Z as an EOF indicator when a file or stream is open in text mode. The control-D behavior is a result of a "cooked mode" keyboard driver, and causes a request for input to return however many bytes have been typed since the last newline or control-D, even if that number is zero, but the control-D never becomes part of the stream. – supercat Jan 27 '23 at 23:37
0

it's ctrl + c in windows, for some silly reason

James
  • 1