Input statement problem

This is a discussion on Input statement problem within the C Programming forums, part of the General Programming Boards category; Hi, Consider the following code; Code: #include <signal.h> #include <stdlib.h> #include <stdio.h> #include <curses.h> int number = 1; void quitter(void); ...

  1. #1
    une
    une is offline
    Registered User
    Join Date
    May 2007
    Posts
    2

    Input statement problem

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

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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
    une
    une is offline
    Registered User
    Join Date
    May 2007
    Posts
    2
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Why would you need threads for such a simple problem?

    http://tldp.org/HOWTO/NCURSES-Progra...OWTO/init.html
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08:12 PM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  4. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 03:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21