Input statement problem

Discussion in 'C' started by enuenu, May 30, 2007.

  1. enuenu

    enuenu New Member

    Joined:
    May 30, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    Consider the following code;

    Code:
    #include <curses.h>
    #include <signal.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int number = 1;
    void quitter(void);
    void quitter (void)
    {
     clear();
     mvprintw(2,10, "Quitting...");
     refresh();
     sleep(4);
     endwin();
     exit(1);
    }
    
    int main ( void )
    {
     initscr();
    
    mvaddstr(1,1,"Enter a number: ");
    refresh();
    
     while(1)
       {
         mvprintw (5,5, "%d", number);
         move(1, 16);
         refresh();
         number = number + 1 ;
         sleep(1);
         signal (SIGINT, quitter);
        //scanw ("%d", number); // NO GOOD - FREEZES LOOP
       }
    }
    This code causes an on-screen counter to count away endlessly. It also provides a prompt for the user to input a new number. I want to accept a number from the user and have it stored in the variable "number", then have the counter display restart from that number instantly. I have not been able to implement a way of reading user input that does not pause execution of the code. After hours of experimenting I have come to the conclusion that getchar(), getch(), sscanf(), scanf(), scanw() or any other input statement will pause execution of the program until the input is made. Everything stops and waits for input, including the counter. I want the counter to never stop, just reset itself as directed by the user's input.

    It almost seems that you need 2 programs running independently in parallel, but somehow connected.

    Any input statement that allows the program to recognize the fact that the user has entered a number stops the program. Its a catch 22 that I cannot overcome.
     

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