Incrementing Twice?

This is a discussion on Incrementing Twice? within the C Programming forums, part of the General Programming Boards category; Hi! So. I am trying to write a program that counts the number of characters entered into the program from ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    Incrementing Twice?

    Hi! So. I am trying to write a program that counts the number of characters entered into the program from the keyboard until EOF. My teacher is having me use the getchar() function. The problem is my program seems to want to increment the count variable twice every time...
    Code:
    #include <stdio.h>
    
    int main( int argc, char *argv[] ) {
    
      int c;
      int count;
    
      count = 0;
    
      while ( ( c = getchar() ) != EOF ){
    
        count ++;
    
      }
    
      printf ("\n%d characters were entered.\n\n", count);
    
      return 0 ;
    }
    So if I entered 3 4 and 5, it would report 6. Does anyone have any ideas why this is happening? Did I just make a really stupid mistake? Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because you entered six characters:
    3
    enter-key
    4
    enter-key
    5
    enter-key

    That's six characters. (Maybe the first two were spaces, but the point still holds.)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,639
    As in you entered 345, then simulated EOF and it reported 6? If you entered 3, then entered 4, then entered 5, the newline characters due to the enters will be counted.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Ah I see. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incrementing character strings? pointers? HELP!
    By ominub in forum C Programming
    Replies: 10
    Last Post: 05-02-2009, 09:03 PM
  2. incrementing in if statements?
    By tomisme in forum C Programming
    Replies: 6
    Last Post: 06-02-2008, 06:17 PM
  3. Incrementing a Dollar amount
    By vamshiy in forum C Programming
    Replies: 5
    Last Post: 06-01-2008, 04:38 PM
  4. incrementing on a specified base
    By canine in forum Windows Programming
    Replies: 2
    Last Post: 12-11-2001, 01:22 PM
  5. Incrementing variables
    By Bazz in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2001, 08:55 AM

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