2009-09-02 29 views
2

我学习C++和我使用Visual C++ Express和同时运行这保持CMD打开,而运行的文件

#include <stdio.h> 

int main() 
{ 
    printf("Hello, World!\n"); 

    return 0; 
} 

cmd窗口关闭如此之快,我无法看到的Hello World 是无论如何,以防止这一点?

回答

5

如果按Ctrl + F5,你将不会被调试器附着 - 然而,它会留在一个“按任意键继续”风格邮件打开。

+0

+1我从来没有想过它:( – AraK 2009-09-02 00:24:14

+0

感谢小费 – Raptrex 2009-09-02 00:46:48

2

在返回之前放置一个getc()。只有按任意键时,程序才会关闭。

1

是的常见解决方案是添加一个从键盘读取输入的语句。这个调用会阻止执行,直到按下某个键。你可以用语句做到像

printf("Hit \"Enter\" to continue\n"); 
fflush(stdin); /* Remove anything that is currently in the standard input */ 
getchar();  /* Wait for the user to hit the enter key */ 
1

,你可以在主月底键入:

system("pause"); 
1

我只是把一个断点return语句。没有代码改变。 :-)

+0

谢谢,我觉得生病使用本 – Raptrex 2009-09-02 00:47:22

1

简单: 写这阻止它

getch(); 
i.e 

main() 

{ 

////// 
Your program 

///////// 

getch(); 

}