How can I end input?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sammyiva
    New Member
    • May 2008
    • 2

    How can I end input?

    Hi, I am learning C by working through K&R using Dev-C++ under windows. Some of the examples have something like

    while((c = getchar()) != EOF)

    to get the input. But I had a problem. Since it takes input from my keyboard... how do I end the input so that I can see the rest of the program run? I did the exercise that had me find the value of the EOF and it showed me -1 but how do I type in a character that has a value of -1? Am I not supposed to be inputting with my keyboard?
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Try Ctrl-D for EOF, or Ctrl-C to kill. Those both work under Linux, and I'm pretty sure Ctrl-C at least carries over.

    Comment

    • sammyiva
      New Member
      • May 2008
      • 2

      #3
      Originally posted by Laharl
      Try Ctrl-D for EOF, or Ctrl-C to kill. Those both work under Linux, and I'm pretty sure Ctrl-C at least carries over.
      Thanks for the answer. I took the Ctrl hint and googled around and found out that Ctrl-Z is EOF under DOS. I thought it was a little funky though that the EOF had to be on a line of its own. What's up with that?

      At least I can finally run the programs. Thanks again.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Ctrl+Z has to be on a line of its own since it signals end of all input.

        However, you have to enter the Ctrl+Z. A lot of people forget that.

        Also, the EOF value does not fit in a char. Use an int.

        Comment

        Working...