-
Find my mistake!
Hi, im a beginner of programming c and this is an exercise from the book The C Programming Language by Bran W. Kernighan and Dennis M. Ritchie, exercise 1-8.
I am trying to simply count the spaces, tabs, new lines of an input. But the problem is, when i compile and run, then type something and enter, nothing comes up. just blanks.
Please find any mistakes or possible solutions!! Thanks.
Code:
#include<stdio.h>
main(){
int line=0,tab=0,space=0;
int input;
while ((input=getchar())!=EOF){
if (input=='\t'){
++tab;
}
if (input==' '){
++space;
}
if (input=='\n'){
++line;
}
}
printf ("%2d%2d%2d",space,tab,line);
}
-
Do you know that you need to signal EOF from the keyboard? CTRL-Z in Windows, I believe, CTRL-D in Linux.
In other words, type in some text, some blanks, some tabs, and newlines - whatever, and when you are finished, do EOF from the keyboard, and the program will print the results.
-
Thanks, Kermit
following your advice, it works now.