注册 登录
编程论坛 C++教室

[原创]如何使C++程序运行完了过后就让它暂时停下来?

hero 发布于 2004-10-05 11:18, 1934 次点击

我是才开始学习C++的,要完成老师布置的作业,现在出现了一个小问题,就是:如何使C++程序运行完了过后就让它暂时停下来?如在C语言里面就有这个getch();这个语句来达到目的。我觉得在C++里面也可以用,但程序总是报错,是不是要包含一个头文件,那头文件应该是什么?谢谢!当然我自己也想了一个办法就是先char a;然后在最后面cin>>a语句来达到,但这个总是打不到getch()那样方便;请各位高手指教,小生谢谢!!!

15 回复
#2
poppylx2004-10-05 12:26
我也知道哦
#3
live412004-10-05 15:24
#include<conio.h>
#4
live412004-10-05 15:29

再一种方法:

#include<cstdlib> //注意没有加.h

void main() { system("pause"); }

#5
live412004-10-05 15:30

两种方法一起用也行:

#include<cstdlib> #include<iostream.h> #include<conio.h>

void main() { cout<<"live41 love Queenie"<<endl; getch(); system("pause"); }

#6
hero2004-10-05 19:33
谢谢版主了!!呵呵!
#7
bcomer2004-10-06 11:37

我按照如上的方法运用了2个暂停

对于GETCH();

效果很不理想

停的位置和预想的很不一样!

以下是一个简单的程序麻烦解释一下:

#include <iostream.h> #include <conio.h> #include <stdlib.h> void convert(int *string,int n) { int i; int tmp; int m=(int)(n/2); for(i=0;i<=m;i++) { tmp=*(string+i); *(string+i)=*(string+n-1-i); *(string+n-1-i)=tmp; } } void main () { int a[9]; int i; int *string; string=a; for(i=0;i<9;i++) { cout<<"\nPlease input a["<<i<<"]"; cin>>a[i]; } for(i=0;i<9;i++) { cout<<'\t'<<a[i]; } cout<<endl; convert(a,9); cout<<"If you want to see the sorted arry,press any key to continue..."; system("cls"); getch(); for(i=0;i<9;i++) { cout<<'\t'<<a[i]; } cout<<endl; }

#8
live412004-10-06 15:00
system("cls");
不要用清屏啊!用system("pause")!清屏谁看得见你的提示!
#9
live412004-10-06 15:02
一般system("pause")都用在最后,getch()没问题,你想要什么解释?
#10
hero2004-10-06 16:41

上楼仁兄的程序我看了一下,你的目的是想输入十个数然后再把他们对称交换后再输出吧,我把你的程序试了一下,你可以把你的程序:

void main () { int a[9]; int i; int *string; string=a; for(i=0;i<9;i++) { cout<<"\nPlease input a["<<i<<"]"; cin>>a[i]; } for(i=0;i<9;i++) { cout<<'\t'<<a[i]; } cout<<endl; convert(a,9); cout<<"If you want to see the sorted arry,press any key to continue..."<<endl; 此处添<<endl

system("cls"); 改为system("pause");

getch(); 把这个语句去掉,并放在后面 for(i=0;i<9;i++) { cout<<'\t'<<a[i]; } cout<<endl;

getch(); 此处添加getch(); }

这样改过后我想应该满足你的要求了。你不妨试试 。

[此贴子已经被作者于2004-10-06 16:42:48编辑过]

#11
live412004-10-06 17:05

不好意思我现在才知道你想说的错误,你的代码是这样: cout<<"If you want to see the sorted arry,press any key to continue..."<<endl; system("cls"); getch();

而运行后变成了这样的顺序,对吗? system("cls"); cout<<"If you want to see the sorted arry,press any key to continue..."<<endl; getch();

这是因为IO的方式不同而引起的,cout<<是C++的,getch()是C的,system()其实算是.net的,或者说Java的,而cout<<是读完了全部在输出,而getch和system是即时输入输出,所以虽然system在cout<<下面但却比cout<<先运行了,所以你要清屏的话,应该这样:

getch(); system("cls"); cout<<"If you want to see the sorted arry,press any key to continue..."<<endl;
#12
live412004-10-06 17:07
不好意思,我楼上的算是废话了,你的问题到底是什么?
#13
kai2004-10-06 23:19

#include <iostream.h> #include <conio.h> #include <stdlib.h> void convert(int *string,int n) { int i; int tmp; int m=(int)(n/2); for(i=0;i<=m;i++) { tmp=*(string+i); *(string+i)=*(string+n-1-i); *(string+n-1-i)=tmp; } } int main () { int a[9]; int i; int *string; string=a; for(i=0;i<9;i++) { cout<<"\nPlease input a["<<i<<"]"; cin>>a[i]; } for(i=0;i<9;i++) { cout<<'\t'<<a[i]; } cout<<endl; convert(a,9); cout<<"If you want to see the sorted arry,press any key to continue..."; cout<<flush; // 清空输出流缓存 system("cls"); // 清屏

// getch(); 这个没有必要,我删掉了

for(i=0;i<9;i++) { cout<<'\t'<<a[i]; // 输出九个元素,这个时候上面那段话讲不再输出。 } cout<<endl; system("pause"); // 在这里起到停住屏幕的作用,以便你能看到结果,在你击Enter 键 之后,程序结束。 return 0; }

// 上面这段程序符合楼主的意思。 关键在于那个 flush.

#14
bcomer2004-10-07 08:31
以下是引用live41在2004-10-06 17:07:56的发言: 不好意思,我楼上的算是废话了,你的问题到底是什么?

不是废话,我有些明白了,谢谢live41.

也谢谢kai版主hero.

#15
bcomer2004-10-07 08:34
以下是引用kai在2004-10-06 23:19:40的发言:

#include <iostream.h> #include <conio.h> #include <stdlib.h> void convert(int *string,int n) { int i; int tmp; int m=(int)(n/2); for(i=0;i<=m;i++) { tmp=*(string+i); *(string+i)=*(string+n-1-i); *(string+n-1-i)=tmp; } } int main () { int a[9]; int i; int *string; string=a; for(i=0;i<9;i++) { cout<<"\nPlease input a["<<i<<"]"; cin>>a[i]; } for(i=0;i<9;i++) { cout<<'\t'<<a[i]; } cout<<endl; convert(a,9); cout<<"If you want to see the sorted arry,press any key to continue..."; cout<<flush; // 清空输出流缓存 system("cls"); // 清屏

// getch(); 这个没有必要,我删掉了

for(i=0;i<9;i++) { cout<<'\t'<<a[i]; // 输出九个元素,这个时候上面那段话讲不再输出。 } cout<<endl; system("pause"); // 在这里起到停住屏幕的作用,以便你能看到结果,在你击Enter 键 之后,程序结束。 return 0; }

// 上面这段程序符合楼主的意思。 关键在于那个 flush.

谢谢kai版主
#16
金多虾2009-08-07 08:36
断点执行可以吧...
1