Writing a program that reads input as a stream of characters until EOF

Discussion in 'C' started by new4c, Jan 9, 2008.

  1. new4c

    new4c New Member

    Joined:
    Jan 3, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    [/SIZE]Writing a program that reads input as a stream of characters until encountering EOF. Have it report the number of uppercase letters and the number of lowercase letters in the input. [/SIZE]

    Here is what I tried but not getting what I'm expecting. Please help me point out why. Thanks a lot
    Code:
    #include<stdio.h>
    #include<ctype.h>
    int main()
    
    {
      
      // Local Declarations
      
         char ch;
         int locnt, u;
         int upcnt, l;
          int index;
      // Statements 
       
      
     printf(" Please enter an uppercase or lowercase letter:\n");
    
    
      while ( (ch = getchar())!= EOF)
    {
                
                if(isupper(ch))
               { for (u = 0; u <= ch; u ++)
                upcnt++;
                printf(" The number of uppercase entered is:\n", upcnt);
                }
                 else (islower(ch));   
                   for ( l = 0; l <= ch; l++)
                 locnt++;
                 printf(" The number of uppercase entered is:\n", locnt);
      
    }//while
    fflush(stdin);
    getchar();
      return 0; 
        
    }//main
    [/SIZE]
     
  2. HowardL

    HowardL New Member

    Joined:
    Aug 5, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    East Coast USA
    So what were you getting?
    Show some output.
    Explain how it differs from what you were expecting...

    You have several things wrong there,, each a mini-discussion in itself.
    'Build' your code in steps.
    Test as you go.
    Then you will see EXACTLY what causes these problems.
    You can solve them as you go along.
    You will learn better habits and not repeat the same things as much.

    I would suggest that you drop back and rebuild that from the beginning.
    Start with only the variables and statements needed for the first thing say the getchar() loop.
    A couple of notes in that regard:
    Code:
        while ( (ch = getchar()) != EOF )
    /* looking for EOF to come from stdin causes endless loop for me in win32
       (not sure about linux) try : '\n' ...that is sent when we press the "ENTER" key */
    /**********/
        /* fflush(stdin);  ***fflush() is intended for stdout, */
        /*  ....for an effective pause/flush try this instead: */
        while( getchar() != '\n')
    Post back progress and next problem.
     
  3. KaiyureBoy

    KaiyureBoy New Member

    Joined:
    May 17, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Yep, thanks, thats good idea.

    I tried some possibilities ( etc.) but with no success.
     
  4. sherma20

    sherma20 New Member

    Joined:
    May 31, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I did'nt read your whole program when I saw that you did not put a scanf just after you asked for the letter. Do that then see what you get
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice