Answer the question
In order to leave comments, you need to log in
How to write a program that produces calculation results after a certain time interval with the ability to change parameters?
Let's say there is a variable a and b. At each calculation step, we add the value of variable b to variable a. It is necessary that the program displays the value of each step with an interval of 1 second. But at the same time, it should be possible to change the variable b during the calculation. That is, you can pause, change the value of the variable and continue counting with the new value. I think that you can put some kind of pause button that will change the value of the flag in a loop
переменные a, b
flag1 = true (кнопка)
цикл с интервалом по времени:
a = a + b
считать значение falg1 (нажал ли кнопку паузы)
если flag1 = false: (нажал)
считать b
flag1 = true
продолжить
вывод переменной a
Answer the question
In order to leave comments, you need to log in
Nothing to pause
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int val =0, timer = 0, freq = 50;
while(true) {
Sleep(1000/freq); //win-функция
timer += 1000/freq;
if (timer > 1000) {
timer -= 1000;
cout << val++ << endl;
}
if (GetAsyncKeyState(VK_SPACE)) { //win-функция
cout << "New val: ";
cin >> val;
}
}
system("pause");
return 0;
}
should be around that. If you hold down the "C" button while the program is running, you will need to enter a new value for the variable b.
while(true){
if(getch() == 32){
std::cout << 'Vvedite novoe b: '; std::cin >> b;
}
std::cout << a << std::endl;
a = a + b;
usleep(1000000);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question