0

I'm trying to find out the number of tabs, spaces and newlines in my C program. The code is :

#include <stdio.h>

void main()
{
    char c;
    int tabs=0 , spaces=0 , nl=0 ;
    printf("Provide the input");
    scanf("%c" , &c);
    while(c != EOF)
    {
        if(c == ' ')
        spaces++;
        else if(c == '\n')
        nl++;
        else if(c == '\t')
        tabs++;
        scanf("%c" , &c);
    }
    printf("blanks:%d\nspaces:%d\nnewlines:%d" , spaces , tabs , nl);
}

Well, I'm expecting it to do just that but the code is just not moving beyond accepting the input phase. What am I doing wrong? Below is what my command line looks like:

[tejas@localhost The_C_Programming_Language]$ cc Exercise_1-8.c

[tejas@localhost The_C_Programming_Language]$ ./a.out

Provide the input seguiofgawie

gweuigwh

e u w   f qw[uwf            
[PHWUO FEFF 
qah fuwpfyh fweor

(I keep pressing the Return key but to no end. Please help. Thank you for reading. This is my first question, hope i didn't do anything wrong...)

EDIT: getchar() doesn't work either, and I'm currently using Fedora 26 and GEDIT as my text editor

EDIT: getchar() works, in order to send the EOF character, one should press Ctrl+D on an empty line and if you want it done in with just one press of the RETURN key, change your loop condition to variable != '\n'. I apologize for spreading misinformation.

Melebius
  • 6,183
  • 4
  • 39
  • 52
  • EOF is not equal to Return. On most systems it is Ctrl + Z. But it still depends. – Nothing Nothing Dec 04 '17 at 12:04
  • 2
    The return key == '\n' (on unix based OS'es), on Windows it's \r\n – Unh0lys0da Dec 04 '17 at 12:05
  • 3
    (1) `scanf("%c" , &c);` never sets `c` to `EOF`. (2) `EOF` is not a valid value of type `char`. Please see `fgetc` and `getchar`, and pay close attention to their return type. (3) Identify the source that taught you to write `void main()` and never trust that person, book or web site again. – n. m. could be an AI Dec 04 '17 at 12:08
  • If you need to read the single key clicks, you have to set the terminal as RAW and then you may use fgetc or read and not scanf. – Sir Jo Black Dec 04 '17 at 12:13
  • It works with `getchar`: https://ideone.com/CZxzKm – mch Dec 04 '17 at 12:14
  • 1
    https://stackoverflow.com/questions/1798511/how-to-avoid-press-enter-with-any-getchar – Sir Jo Black Dec 04 '17 at 12:15
  • My suggestions are Linux based! Are you using Linux? – Sir Jo Black Dec 04 '17 at 12:18
  • @SirJoBlack Yes. Also can you please tell how to write code in comments and how to enter a newline? I've succesfully made a fool of myself about 4 times already – Tejas Garhewal Dec 04 '17 at 12:20
  • What do you mean with the request: "How do I write code in comments?" – Sir Jo Black Dec 04 '17 at 12:24
  • Like how @n.m. wrote scanf("%c" , &c); – Tejas Garhewal Dec 04 '17 at 12:26
  • `void main()`? I can sense something ancient. – iBug Dec 04 '17 at 12:34
  • @n.m. Regards to (3)It's what we've been doing in college the entire semester... – Tejas Garhewal Dec 04 '17 at 12:42
  • @iBug I'm just doing what i was taught at college last semester... we also used turbo C and windows 7... This is my first I/O program in linux... god help me for the future... – Tejas Garhewal Dec 04 '17 at 12:44
  • I hear Turbo C was made mandatory in India by some misguided national school board officials. This was a really unfortunate decision. Try to stay as far away from Turbo C as you can. On Linux, use `-std=c99 -pedantic -Wall -Werror` compiler options to compile all your C programs, and fix every warning religiously (don't just use type casts to silence them). – n. m. could be an AI Dec 04 '17 at 13:17
  • Regadring "getchar() doesn't work either": this is not a valid complaint, you need to show your code. In a Linux terminal, press Ctrl+D on an empty line to send eof. – n. m. could be an AI Dec 04 '17 at 13:20
  • To show code in comments, use backticks: \`int main\` -> `int main` – n. m. could be an AI Dec 04 '17 at 13:21
  • @n.m. okay the Ctrl+D made it work... I was wrongly blaming getchar(). I'll re edit my question to reflect this to not spread misinformation... thank you for taking the time. Thank you for the compiler flags. – Tejas Garhewal Dec 04 '17 at 13:26
  • If one of the answers given fixed your issue, you should [accept that answer](https://stackoverflow.com/help/accepted-answer). – dbush Dec 04 '17 at 13:51
  • @dbush Both the answers recommend changing the type of my `c` variable. It doesn't have to be changed. https://ideone.com/tTR80n#stdin – Tejas Garhewal Dec 05 '17 at 04:43

2 Answers2

2

There is a small error here that you are commiting, by comparing the scanned char value to EOF. Quoting another answer

EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.

However, since you are reading from stdin, you won't be able to enter it as a single char since it will be considered as 2 characters and will not produce the desired result. In a nutshell, your program is behaving in the desired way. In order for it to exit input mode, just change your while condition to something which you are able to meet.

funny_geek
  • 41
  • 6
0

i think, a character given by the user cannot be compared to EOF(end of file) as you are not dealing with any file i.e., you have not opened any file in order to verify its end.

yogeswaran
  • 11
  • 2