Like Tree1Likes
  • 1 Post By cyberfish

how to make a counter

This is a discussion on how to make a counter within the C Programming forums, part of the General Programming Boards category; Ok, guys I had an idea, I want to make a program that counts how many characters you type, but ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    4

    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
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int cont;
    char sig[1];
    
    int main(void){
    printf("start counting");
        for(;;){
        cont=0;
        scanf("%c", sig);
        if(sig!="q"){
            cont++;}
        else{break;}
    }
    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
    Location
    Canada
    Posts
    3,181
    There is no way to do that in standard C unfortunately. Input only goes into C's IO buffers after you press enter.
    dennis.cpp likes this.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    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
    Posts
    1,066
    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:
    Code:
    #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)
            count++;
    
        printf("\nYou've entered %d characters\n", count);
    
        return 0;
    }
    Code:
        if(sig!="q"){
    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, 02:33 PM
  2. Counter
    By jackirl in forum C++ Programming
    Replies: 5
    Last Post: 11-27-2011, 11:01 AM
  3. Trying to make this counter output something different
    By NinjaFish in forum C Programming
    Replies: 14
    Last Post: 02-16-2011, 04: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, 02:17 AM

Tags for this Thread


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