我不知道如何在中间停止程序有人可以帮助我我希望我的程序停止定义的秒数,例如 3 秒或 6 秒,如果您按任意键,它会立即开始运行。
这对你有帮助吗?
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
// Sleeping Mechanism
for(auto i = 1; i <= 2; ++i)
{
cout << "Code pauses for 1 second\n";
sleep(1); // Specify the number of seconds you want this block to sleep for
}
// Further code is executed once a particular key is pressed
char ch;
cin >> ch;
while (ch != 'e')
{
cout << "Pressed " << ch << "\n";
cin >> ch;
}
// Some code to be executed after pressing e only
cout << "e pressed\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您等待按下特定键的第二部分只是一种解决方法,对于您正在寻找的内容来说并不是一个很好的解决方案。