Thread: Input statement problem

  1. #1
    Registered User
    Join Date
    May 2007

    Input statement problem

    Consider the following code;
    #include <signal.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <curses.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.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Quote Originally Posted by une View Post
    It almost seems that you need 2 programs running independently in parallel, but somehow connected.
    Congratulations on discovering a purpose for separate threads of execution within the same program.

    Since it appears from your code that you're on a *nix system, you could use pthreads. If you wish this to work on Windows, there is a pthreads-for-Windows library floating around somewhere.

  3. #3
    Registered User
    Join Date
    May 2007
    Thanks. I have not used threads before but had an idea that I may need to use them to solve this problem. I was hoping to work out a solution without using threads.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Why would you need threads for such a simple problem?
    Just put ncurses into raw() mode and you'll be able to read input without pausing.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM