Link to home
Start Free TrialLog in
Avatar of computerese
computerese

asked on

Problem with STREAM INPUT for C Program

I'm having a problem with a program I wrote that should show the average number of letters per word after EOF (Ctrl 'Z') is entered. I think the program is reading the input as a stream of characters OK. My average is not working. I'd appreciate if someone could identify where I'm going wrong.

----------------------------------------HERE IS MY CODE-----------------------------------------------

#include <stdio.h>

int main()
{

    int ch; /* some character */
    int count=0; /* count letters */

    printf("Enter input ending with ctrl 'Z' or ctrl 'D':\n");

    ch = getchar(); /* get first input */

    /* loop till eof entered */
    while(ch != -1)
    {

       // check for now space
       if(ch > ' ')
         count++; /* increment count */

       ch = getchar(); /* get next input */
    }

    /* report number of letters */
    printf("Number of letters %d\n",count);


    printf("\n");
    getchar(); /* pause */

    return 0;
}


Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi computerese,

What doesnt work?

Paul
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of computerese
computerese

ASKER

Thanks Mr. Caswell, that fixed my problem!