Like Tree1Likes
  • 1 Post By nonpuz

getchar()

This is a discussion on getchar() within the C Programming forums, part of the General Programming Boards category; Im sorry to bother anyone. I just started programming in C,and got a K&R to guide me. The thing is.I ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    35

    getchar()

    Im sorry to bother anyone.
    I just started programming in C,and got a K&R to guide me.
    The thing is.I made code that counts spaces, tabs and new lines.
    Now the problem is,once i build and run everything,when i try to type something,it doesen't actually count anything.

    Im just looking for some help.It would be really great if you could explain this to me.Sorry about this,i didn't have time to attach the actual file.
    Here is the code:

    Code:
    #include <stdio.h>
    
    main()
    {
        int c, fl, fn, fb;
    
        fl = 0;
        fn = 0;
        fb = 0;
    
    
        while ((c = getchar()) != EOF) {
            if (c == ' ')
             ++fl;
            if (c == '\t')
             ++fn;
            if (c == '\n')
                ++fb;
    
       }
           printf ("d %d %d\n", fl, fn, fb);
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Looks okay to me.
    Are you sure that the console window isn't just closing before you get to see the results?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    It just opens the console and lets me write.But when i do type in text ,it doesen't do anything.Just gives me new line to write again.
    The console is open until i close it manually.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    One more thing.When i tryed to make like a simple copy program(copies whatever i type).If i use the while loop the program doesen't work,but if i were to use the for loop.It works perfect.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Filster7 View Post
    It just opens the console and lets me write.But when i do type in text ,it doesen't do anything.Just gives me new line to write again.
    The console is open until i close it manually.
    In the print line of code, you're missing one % before the first d in the format string.

    You are pressing the TAB key for tab counting, and not \t etc., right?

    Works for me. Three spaces, three tabs, and one newline:

    Name:  output1.PNG
Views: 117
Size:  4.2 KB

    For copying a program, use a binary file mode. Read in a block of bytes, and write them out. Wash, rinse, repeat.

    About those variable names - ditch them, for the sake of all our sanity - whatever we have left. Seriously, you were indulging in holiday cheer at the time, hopefully.
    Last edited by Adak; 12-24-2012 at 05:05 PM.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    I did forget the %.I made that mystake ,thing is ,it still doesen't work for me.The TAB thing though,your right.
    The thing is this doesen't end.The program just keeps going.Its like im in a notebad typing stuff.Nothing else.

    Thank you for your help though.You did show me some ways .

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    It would help if you talked about "doesn't work" in disgusting detail. If people are getting vastly different results from you, now is an excellent time to get paranoid. If you get strange output, show that. If you enable a compiler option, show that. Do as much as you can. We're not psychic.

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    473
    The problem I think is that hes not "ending the input" or generating an EOF character.

    In windows you need to type Ctrl+Z after typing, once you do that THEN the program will output (your while() condition doesn't end until it hits EOF). In linux you
    would do this by pushing Ctrl+D.
    Adak likes this.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I showed the EOF char ^Z, being used as EOF on Windows, but I didn't specify the EOF as Ctrl+Z, which I should have. Thanks nonpuz for clarifying that.

    Filster7 are you ending your input with Ctrl+z, and STILL it won't terminate?

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    It did work,thanks alot Adak.Your the greatest .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why two getchar() ?
    By ekosix in forum C Programming
    Replies: 2
    Last Post: 06-05-2010, 01:13 PM
  2. Using getchar()
    By countchocula in forum C Programming
    Replies: 13
    Last Post: 04-23-2008, 08:07 AM
  3. getchar() and '\b'
    By snfiddy in forum C Programming
    Replies: 2
    Last Post: 11-30-2005, 04:52 PM
  4. getchar()
    By linuxdude in forum C Programming
    Replies: 7
    Last Post: 09-05-2004, 09:17 PM
  5. getchar
    By TyrioN in forum C Programming
    Replies: 5
    Last Post: 09-01-2004, 07:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21