#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    Smile Counting the number of characters from input


    Hello, I am a newbie to this forum. :) I am trying to learn C programming from the book "The C Programming Language 2nd Edition" by Brian Kernighan. I am using the Dev-C++ compiler, running on Windows 7. So here's a bit of trouble I am facing.
    I am trying to count the number of characters from an input provided by the user. As per the instructions, this is the piece of code I am supposed to use:
    Code:
    #include <stdio.h>
    
    main()
    
    {
    	long nc;
    	
    	nc = 0;
    	while (getchar() != EOF)
    	    ++nc;
    	printf("%ld\n", nc);
    }
    When I run the program after compiling it and provide an input, nothing happens. What I get is a new line to provide another input. I am not exactly sure where I am going wrong. Similar problem occurs when I use this code:
    Code:
    #include <stdio.h>
    
    main()
    
    {
    	double nc;
    	
    	for (nc = 0; (getchar() != EOF); ++nc)
    	;
    	printf("%.0f\n", nc);
    }
    I would be grateful if someone looked into the problem. Thanks in advance! :)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    I don't see anything wrong with the program. After you type the input text, press <CTRL+Z> to mean EOF (since you're looping until EOF is found)

    Code:
    $ ./a.out
    hello
    6
    $ ./a.out
    hello
    world
    12
    $
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by noobie1000
    I don't see anything wrong with the program. After you type the input text, press <CTRL+Z> to mean EOF (since you're looping until EOF is found)

    Code:
    $ ./a.out
    hello
    6
    $ ./a.out
    hello
    world
    12
    $
    The number of characters just doesn't show up. Here's what I get:
    Code:
    root@kali:~# ./charcount
    hello
    ^Z
    [1]+  Stopped                 ./charcount
    Sorry if I sound too stupid.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    I'm sorry. It's my mistake. It should be <CTRL+D> and not <CTRL+Z>.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by noobie1000
    I'm sorry. It's my mistake. It should be <CTRL+D> and not <CTRL+Z>.
    Thanks a lot! Worked like a charm on Linux! How do I make it work on windows?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Try ctrl+z

IMN logo majestic logo threadwatch logo seochat tools logo