the code is like this:
I entered "this is a test" then followed by control+z, after hitting enter it does not give me anything, only the cursor moved to the next line. anything wrong?
If there is 11 characters and 4 words, it should give me an average of 2.75, but what I got was 2.00000...what went wrong?
thanks for help!
Code:
#include<stdio.h>
int main(void)
{
float average;
int flag=0;
int c=0;
int word=0;
char ch;
printf("pls enter your sentence:");
while ((ch=getchar())!=EOF)
{
if(ch!=' '&&ch!='\n')
c++;
if(ch!=' '&&ch!='\n'&&flag==0)
{
word++;
flag=1;
}
if((ch=='\n'||ch==' ')&&flag==1)
flag=0;
}
average=c/word;
printf("word are %d\n",word);
printf("characters are %d",c);
printf("the average is %f",average);
system("pause");
return 0;
}
If there is 11 characters and 4 words, it should give me an average of 2.75, but what I got was 2.00000...what went wrong?
thanks for help!