C coding to print the value until next Key Stock?

Discussion in 'C' started by jagadesh_61, Sep 27, 2010.

  1. jagadesh_61

    jagadesh_61 New Member

    Joined:
    Sep 27, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Here i need to get Keybaord input but process should not wait for keybaord input
    whenever Key is pressed corresonding action should take place

    eg. if first time i pressed '+' the i value should be incremented until
    next key is pressed and if '-' is pressed then i value should be decremented
    until next key is pressed. pls refer & check complete the following code.
    Code:
    void main()
    {
    int i=0;
    char ch='*';
    while(1)
    {
        /* Here i need to get Keybaord input but process should not wait for keybaord input
           whenever Key is pressed corresonding action should take place 
            
        eg. if first time i pressed '+' the i value should be incremented until
                next key is pressed    and if '-' is pressed then i value should be decremented
            until next key is pressed.
        
            */
        KeyInput = ReadKeybaordInput(); 
        
        if(KeyInput == '+')
            printf("\n i=%d",i++);
        else if(KeyInput == '-')
            printf("\n i=%d",i--);
        else if(KeyInput == '*')
            printf("\n i=%d",i);            
        else if(KeyInput == '/')
            exit(0);
    
    }
    }
     
    Last edited by a moderator: Sep 27, 2010
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    There is no standard 'C' way to do this. It is possible to do it but it is operating system dependent. So please let us know your operating system.

    Jim
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    one way is this

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main(){
        int i=0;
        
    printf("enter + to increase numbers ,- to decrease any other to pause and / to exit");
    printf("\nstarting from %d\n",i);
    char a='o';
    while(a!='/'){
        while(!kbhit()){
            if (a=='+') 
                    printf("\n%d",++i);
            else if (a=='-') printf("\n%d",--i);
        }
        a=getch();
    }
    
    printf("\nyou pressed / and stopped the process!!!");
    getchar();
    }
    
    it worked in windows with wxDevcpp compiler.
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice