How can I input an EOF value to end a while loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Haris Radoncic
    New Member
    • Apr 2011
    • 5

    How can I input an EOF value to end a while loop?

    I picked up the "C Programming Language" and have been learning it bit by bit but Im alittle confused about EOF. I example 1-8, we need to write a program that outputs the number of tabs, blanks, and newlines entered into the program, using a while loop to control when the program performs the outputs. I know that EOF is a negative value (-1 apparently), but how do I enter it into the program so that the while loop ends since every keystroke is interpretted as a character. Here's the code:

    Code:
    #include <stdio.h>
    
    main(){
           
           int c, tab = 0, blank = 0, nl = 0;
           printf("EOF = %d", EOF);
           printf("\n\n");
           while ((c = getchar()) != EOF)
                 if (c == '\n')
                    ++nl;
                 else if(c == '\t')
                    ++tab;
                 else (c == ' ');
                    ++blank;
           printf("\nNew Lines: %d", nl);
           printf("\nTabs: %d", tab);
           printf("\nBlanks: %d", blank);
           }
    Last edited by Banfa; Apr 21 '11, 09:34 AM. Reason: Added [code]...[/code] tags round the code, please use them in future.
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    If you're typing input from the command prompt you type CTRL+Z for windows or CTRL+D for linux

    If you're reading from a file it will just get an EOF when the end is reached automatically

    Comment

    • Haris Radoncic
      New Member
      • Apr 2011
      • 5

      #3
      Ok, when I do Ctrl-Z, the program automatically exits instead of just exiting the loop and printing out the output statements, any suggestions?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        The problem you are encountering is because the stdin and stdout streams normally connected to the keyboard and console are continuous, they do not end so you never get an EOF like you do say for a file where you get an EOF at the end of the file.

        Either make the program look out for some sentinel character you can easily detect to end it or make the program read an actual file that will result in an EOF.

        BTW while you code is correct it would generally be considered best practice today to use rather more braces {} than you have to delineate the blocks of code executed by the while statement and all the if statements.

        Comment

        • Haris Radoncic
          New Member
          • Apr 2011
          • 5

          #5
          I got it to run perfectly with the Ctrl-Z advice. I modified the code slightly to to include printing tabs, new lines, blanks, and the total number of key presses.

          Code:
          #include <stdio.h>
          #include <conio.h>
          
          main(){
                 
                 int c, tab = 0, blank = 0, nl = 0, character = 0;
                 printf("EOF = %d", EOF);
                 printf("\n\n");
                 while ((c = getchar()) != EOF){
                       if (c == '\n')
                          ++nl;
                       else if(c == '\t')
                          ++tab;
                       else if(c == ' ')
                          ++blank;
                       else (c == ' ');
                       ++character;
                       }
                 printf("\nNew Lines: %d", nl);
                 printf("\nTabs: %d", tab);
                 printf("\nBlanks: %d", blank);
                 printf("\nTotal Keyboard Presses: %d", character);
                 getch();
                 }
          Quick question though, am I basically a few steps away from making a word count program? I can modify it to basically enter a file name (as suggested above) where it prints the information once it reaches the end of the document. I'll try it out but i dont see why it cant work. Programming is fun isnt it?

          Comment

          Working...