在C ++中有一个体面的等待函数吗? [英] Is there a decent wait function in C++?
问题描述
我在C ++中学到的第一件事是
#include< iostream>
int main()
{
std :: cout<<Hello,World!\\\
;
return 0;
}
为了防止这种情况,我不得不去记事本,并保存
helloworld.exe
pause
ase
helloworld .bat
当我需要创建一堆小测试程序时,这很乏味,在我的大多数测试程序的结尾放置
while(true);
,所以我可以看到结果。是否有更好的等待功能我可以使用?解决方案您可以要求用户在关闭程序之前按Enter键类似这样的工作。
#include< iostream>
int main()
{
std :: cout< Hello,World\\\
;
std :: cin.ignore();
return 0;
}
cin读取用户输入,告诉程序只是忽略输入。程序将在用户输入后继续。
One of the first things I learned in C++ was that
#include <iostream> int main() { std::cout<<"Hello, World!\n"; return 0; }
would simply appear and disappear extremely quickly without pause. To prevent this, I had to go to notepad, and save
helloworld.exe pause
ase
helloworld.bat
This got tedious when I needed to create a bunch of small test programs, and eventually I simply put
while(true);
at the end on most of my test programs, just so I could see the results. Is there a better wait function I can use?解决方案you can require the user to hit enter before closing the program... something like this works.
#include <iostream> int main() { std::cout << "Hello, World\n"; std::cin.ignore(); return 0; }
The cin reads in user input, and the .ignore() function of cin tells the program to just ignore the input. The program will continue once the user hits enter.
这篇关于在C ++中有一个体面的等待函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!