character counting

Hi, anyone can help on it?

i am using mac,Xcode to run the very simple code as below, but it shows nothing? why? did i miss anything? would it be different if i run it with other machine or other compiler?


int main ()
{
double nc;
for (nc=0; getchar() !=EOF; ++nc)
//printf("%.0f\n", nc)
;

printf("%.0f\n", nc);

}
Console doesn't have end of file. getchar will just wait until you enter something and return that.
i did put something in.

i tried

int main ()
{
double nc;
for (nc=0; getchar() !=EOF; ++nc)
printf("%.0f\n", nc)
;
//printf("%.0f\n", nc);
}
the put in "oooo"
i will have result of below
0
1
2
3
4

my understanding is the second program count letters one by one, i would expect the first program just give me one final result, that is "4" if i type in "oooo". .....
but it shows nothing...
You're not listening.

Take your code:
1
2
3
4
5
6
7
8
int main ()
{
  double nc;
  for (nc=0; getchar() !=EOF; ++nc)
    printf("%.0f\n", nc);

  printf("foo");//and add this line here.
}


If you can somehow get "foo" printed (without changing the code), everything is great. If not, it means that the condition always returns true and is thus broken.

When you come to that realisation, you have to ask yourself what kind of condition you want. If it's to go until the end of line, replace EOF with '\n'.
Topic archived. No new replies allowed.