Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collect keyboard input inside a loop in c++

Tags:

c++

linux

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
}
like image 457
Learning is a mess Avatar asked Feb 01 '26 19:02

Learning is a mess


1 Answers

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.

like image 177
V. Semeria Avatar answered Feb 04 '26 11:02

V. Semeria