Thread: Inkey$ in C++?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Inkey$ in C++?

    I've looked around teh world and I stumbled on this code here, but I want to beable to make this loop printf the a++ postincrement while at the same time reading to see if a key has been pressed, if your familiar with qbasic, where you put n$ = inkey$ in a loop and it performs that loop w/o any type of pause until a desired key has been pushed. So this getchar waits for me to press a key, and once I press a key it prints a++, but I just want it to print a++ continiously while i can at the same time send/read keystrokes in the program, so im kinda like tryin to understand the concept of multithreading it....
    I was looking at the NCURSES, but not sure if that does something similar or if there are any other approaches, dankeshen, and a good night. BTW This is being compiled on a FreeBSD 5.4, with gcc (GCC) 3.4.2 [FreeBSD] 20040728
    here is the code:

    Code:
    #include <curses.h>
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    
    int main(void)
    {
        struct termios oldt,
                       newt;
        int            ch,a=0;
    
        while(1) {
           a++;
           printf("%d\n", a);       // i want this to keep printing but i still want some type of getchar
                                    // not pausing the arithmetic
           tcgetattr( STDIN_FILENO, &oldt );
           newt = oldt;
           newt.c_lflag &= ~( ICANON | ECHO );
           tcsetattr( STDIN_FILENO, TCSANOW, &newt );
           ch= getchar();           // this here pauses so a++ wont print continiously ;(
           tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
           printf("%c",ch);
        }
    
        return ch;
    }
    Last edited by reefa; 10-02-2005 at 09:32 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    look into the non standard function kbhit()

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > look into the non standard function kbhit()
    Better not, that's conio, and this isn't even DOS.

    There's a similar non-blocking key reading function in ncurses, which is included but unused.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well if one was to search the linux board, one would find a kbhit() written for linux

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Thantos
    Well if one was to search...
    Most posters lose you right about here.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by pianorain
    Most posters lose you right about here.
    We should have a 'Best Posts Ever' thread, because that'd hit no. 1.
    Nice one
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For those who know Qbasic:inkey$ in c++
    By Ivan! in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2003, 09:25 AM