3

I have a program running a numerical calculations with precision increasing in time. It does so for different values of some parameters. The precision I need for each results depends on the value of these parameters, in a way totally unknown to me.

To get enough precision for each value, I am thinking about a program/loop that would cut a calculation and move on to new values of the parameters if the user hits the keyboard.

Schematically:

//initialise parameters
while( parameters_in_good_range){
     while( no_key_pressed){
         //do calculation
     }
     //update parameters
}
GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
  • 1
    What platform are you on? – John Zwinck Sep 14 '16 at 11:52
  • 6
    There's no way to poll the keyboard in standard C++, you need to rely on OS specific functions. – Mark Ransom Sep 14 '16 at 12:00
  • 1
    @MarkRansom not totally agree with you: since c++11, you can create thread and have some control on it, and have some basic synchronization mechanism. So you can have a thread which do calculation and a second one which read standard input. When second receive an input, it can tell to the other that it has to quit – Garf365 Sep 14 '16 at 12:04
  • @JohnZwinck: Linux – Learning is a mess Sep 14 '16 at 12:10
  • @Garf365 but *standard input* is different from *keyboard input*. – eerorika Sep 14 '16 at 12:10
  • 1
    @close-voter: it's perfectly clear what this question is asking. It says `while(no_key_pressed){/*do calculation*/}`. – GreenAsJade Sep 14 '16 at 12:14
  • @user2079303: right, I misread the comment about "keyboard input", I apologize. But you can poll standard input with modern c++ in a portable way – Garf365 Sep 14 '16 at 12:18

1 Answers1

3

On Windows, this program will loop until a keyboard key is pressed :

#include <conio.h>

void main()
{
    while (1)
    {
        if (_kbhit())
        {
            break;
        }
    }
}

On linux, have a look at this answer. It tells you how to use ncurses to do what you want.

Community
  • 1
  • 1
V. Semeria
  • 3,128
  • 1
  • 10
  • 25