Thread: how to make a counter

  1. #1
    Registered User
    Join Date
    Jun 2012

    how to make a counter

    Ok, guys I had an idea, I want to make a program that counts how many characters you type, but I don't know how to make an input of one character at a time without pressing enter, neither how to make a display that changes every time you press a key. so far this is what I got of code
    #include <stdio.h>
    #include <stdlib.h>
    int cont;
    char sig[1];
    int main(void){
    printf("start counting");
        scanf("%c", sig);
    printf("%d", cont);
    I thought it would work fine but it doesn't quit if I press "q", plus I have to pres enter for this program to count one character.

  2. #2
    Registered User
    Join Date
    Dec 2006
    There is no way to do that in standard C unfortunately. Input only goes into C's IO buffers after you press enter.

  3. #3
    Registered User
    Join Date
    Mar 2011
    cyberfish is right because it the behavior is system dependent so you have to use system API's to do what you want. if you are using Visual studio on windows, you can use "conio.h" to get that behavior. look up _kbhit and getch. if you are on Linux you can set your terminal to raw mode using the termios functions.. google 'linux termios' and look at the function cfmakeraw. if you are on apple, i have nothing for you.

  4. #4
    Registered User
    Join Date
    May 2012
    Quote Originally Posted by Tollme View Post
    Ok, guys I had an idea, I want to make a program that counts how many characters you type
    If you only want to count the characters and not the number of key presses, it's rather easy:
    #include <stdio.h>
    int main(void)
        int count = 0;
        printf("Start counting (Quit with CTRL-D in Unix/CTRL-Z in Windows)\n");
        while (getchar() != EOF)
        printf("\nYou've entered %d characters\n", count);
        return 0;
    That's just wrong in any case because if you want to compare characters you have to use single quotes. For comparing strings you have to use strcmp.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I make a percentage counter?
    By Once-ler in forum C Programming
    Replies: 7
    Last Post: 11-29-2011, 03:33 PM
  2. Counter
    By jackirl in forum C++ Programming
    Replies: 5
    Last Post: 11-27-2011, 12:01 PM
  3. Trying to make this counter output something different
    By NinjaFish in forum C Programming
    Replies: 14
    Last Post: 02-16-2011, 05:13 AM
  4. Odd/Even counter.
    By fyoung91 in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2010, 12:02 PM
  5. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 03:17 AM

Tags for this Thread