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
Consider the following code;


#include <curses.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

int number = 1;
void quitter(void);
void quitter (void)
 mvprintw(2,10, "Quitting...");

int main ( void )

mvaddstr(1,1,"Enter a number: ");

    mvprintw (5,5, "%d", number);
    move(1, 16);
    number = number + 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 00:45.