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;
}
--------------------------
#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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Mr. Caswell, that fixed my problem!
What doesnt work?
Paul