让c++程序暂停的方法有哪些?

来源:百度知道 编辑:UC知道 时间:2024/05/31 06:18:45
例如说我用一个for循环循环三次,第二次的时候我想让程序暂停,按任意键继续,有哪些方法可以实现,getch我试过,似乎不行,望高手指点
我用getch是编译总是出错,出错信息如下: implicit declaration of function `int getchar(...)',程序循环时,我是为了显示一些信息,所以暂停

1.程序末尾即return 0;前,加 while(1);
2.程序末尾即return 0;前,加 scanf("\n");(需加头文件<cstdio>)
3.程序末尾即return 0;前或者需暂停处,加 system(“pause”);(需加头文件<iostream>)(推荐,即你想要的“按任意键继续”)

程序暂停:
在C++里使用
#include <conio.h>
getch();
或者
#include <cstdio>
getchar();

在windows中,还可以使用
#include <cstdlib>

system("pause");

如果单纯想要查看结果,还可以使用sleep
windows中
#include <windows>

Sleep(int);
在linux中
#include <unistd.h>
sleep(int);

判断就行了嘛
for (int i = 0; i < 3, i++)
{
if (i = 2)
{
getch();
}
}

system("pause");
有时得加头文件#include<windows.h>

用getch需要包含头文件#include<conio.h>