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
}
7
  • 1
    What platform are you on? Commented Sep 14, 2016 at 11:52
  • 6
    There's no way to poll the keyboard in standard C++, you need to rely on OS specific functions. Commented Sep 14, 2016 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 Commented Sep 14, 2016 at 12:04
  • @JohnZwinck: Linux Commented Sep 14, 2016 at 12:10
  • 1
    @close-voter: it's perfectly clear what this question is asking. It says while(no_key_pressed){/*do calculation*/}. Commented Sep 14, 2016 at 12:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Why not while(!_kbhit()) {/*do stuff*/} ?
it doesn't really matter either way since OP is on Linux and not Windows
Link to a linux answer added above.
@V.Semeria a link isn't good enough, it isn't an answer unless it actually contains the relevant information.
It contains an example of ncurses that solves the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.