C coding to print the value until next Key Stock?

jagadesh_61's Avatar, Join Date: Sep 2010
Newbie Member
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 shabbir; 27Sep2010 at 20:19.. Reason: Code blocks
jimblumberg's Avatar
Ambitious contributor
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
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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 like this