key press in c plz help thanks

This is a discussion on key press in c plz help thanks within the C Programming forums, part of the General Programming Boards category; hi i'm trying to write a program that increases numbers when the 'space' key is pressed and held down, stops ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    13

    key press in c plz help thanks

    hi i'm trying to write a program that increases numbers when the 'space' key is pressed and held down, stops increasing when the space key up. I got the numbers incrementing when a user presses the space key once and to stop when the space key is pressed again but i need to have it so that the numbers increment while the space key is held down and stops incrementing when the user stops pressing the space key...below is my program. any help would be appreiciated thanks (code written in turbo c++ v3 compiler) Thanks to anyone that helps!!!

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<dos.h>
    #include<graphics.h>
    
    int SetGraph (void);
    void Counter (void);
    
    int main (void)
    {
    int key;
    SetGraph();
    setcolor(WHITE);
    outtextxy(300,340, "Press Space Bar to deliver fuel");
     key = getch();
    while(!kbhit())
      {
      Counter();
      }
    
    }
    
    
    
    int SetGraph (void)
    {
    int gdriver = DETECT, gmode, errorcode, Gworks = 1;
    initgraph(&gdriver, &gmode, "\\tc\\bgi");
    errorcode = graphresult();
    if(errorcode != grOk)
    {
    		printf("Graphics error: %s\n", grapherrormsg(errorcode));
    		printf("Press any key to halt:");
    		getch();
    		}
    		return Gworks;
    
    }
    
    void Counter (void)
    {
     char *buf;
     float sale =000.00;
     float litres = 0000.0;
    
    
    do
    {
      setfillstyle(SOLID_FILL,WHITE);
      bar(403,206,560,183);
      setcolor(BLACK);
      settextstyle(DEFAULT_FONT, HORIZ_DIR,3);
      sprintf(buf, "%3.2f",sale);
      outtextxy(408,185,buf);
      delay(150);
      sale+=030.50;
    
      bar(403,256,560,233); 
      sprintf(buf,"%4.1f",litres); 
      outtextxy(408,235,buf);
      delay(150);
      litres+=444.5;
      bar(403,306,560,283);
      outtextxy(408,285,"72.8 p");
    
    }while(!kbhit());
    
    getch();
    
    return 0;
    }
    last code had error sorry

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What did you do to the other post? I already answered this question.
    Code:
    while( !kbdhit( ) ); /* nothing is pressed */
    
    while( kbdhit( ) )
    {
        key = getch( );
        if( key == ' ' )
            counter++;
        else
            counter=0;
        printf("counter == %d\n", counter );
    }
    Something like that should work. Again, your program is heavily system dependant, which isn't to say it's necessarily bad, just that it is, and these are not standard functions.

    If I remember correctly, kbdhit returns 0 when nothing is hit, and non-zero when a key is struck.

    Code:
    char *buf;
    ...
    sprintf(buf, "%3.2f",sale);
    You never allocate memory, so 'buf', just points to some random place in memory, and when you try to use it, BadThings(TM) happen.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 05:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 05:42 PM

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