Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Input statement problem (http://www.go4expert.com/forums/input-statement-problem-t4475/)

enuenu 30May2007 08:36

Input statement problem
 
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.


All times are GMT +5.5. The time now is 11:46.