关于《C++程序设计原理与实践》第3章例子的一个问题

雨夜独行的人 2014-01-19 08:53:22
本人菜鸟,现正在学习C++。《C++程序设计原理与实践》第3章有一个例子,代码如下:[/b]

#include<iostream>
#include<string>
#include<vector>
#include <algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open(){ char ch; cin >> ch; }
int main() //C++ Programs start by executing the function main
{
cout << "Please enter a floating-point value:";
double n;
cin >> n;
cout << "n==" << n
<< "\nn+1==" << n + 1
<< "\nthree times n==" << 3 * n
<< "\ntwice n==" << n + n
<< "\nhalf of n==" << n / 2
<< "\nsquared root of n==" << sqrt(n)
<< endl;
}


我执行后发现输入数字后再按回车会退出程序窗口,而不是输出计算结果。不知道为什么。请各位大神不吝赐教!谢谢!
...全文
284 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
u011043553 2014-02-10
  • 打赏
  • 举报
回复
引用 8 楼 mujiok2003 的回复:
[quote=引用 7 楼 u011043553 的回复:] 可以啊
ctrl+F5的吧?[/quote]直接f5啊
harold2007 2014-01-27
  • 打赏
  • 举报
回复
学习了~楼上的都把执行结果给出来了
mujiok2003 2014-01-26
  • 打赏
  • 举报
回复
引用 7 楼 u011043553 的回复:
可以啊
ctrl+F5的吧?
u011043553 2014-01-26
  • 打赏
  • 举报
回复
可以啊
mujiok2003 2014-01-22
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<vector>
#include <algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open(){ char ch; cin >> ch; }
int main() //C++ Programs start by executing the function main
{
  cout << "Please enter a floating-point value:";
  double n;
  cin >> n;
  cout << "n==" << n
    << "\nn+1==" << n + 1
    << "\nthree times n==" << 3 * n
    << "\ntwice n==" << n + n
    << "\nhalf of n==" << n / 2
    << "\nsquared root of n==" << sqrt(n)
    << endl;
    keep_window_open(); 
}
mujiok2003 2014-01-22
  • 打赏
  • 举报
回复
keep_window_open需要调用.
reinfocedconcrete 2014-01-22
  • 打赏
  • 举报
回复
回车后后面的代码也执行了,执行完就自动退出了,速度太快你看不到。你这样写其他的例子,执行时也是一闪而过。解决方法如1楼。
max_min_ 2014-01-20
  • 打赏
  • 举报
回复
在你的main函数结束前加上一个停顿就好了! getchar()或者while(1) 出现你说的情况是因为程序运行完了!
孩皮妞野 2014-01-20
  • 打赏
  • 举报
回复
如果你使用Ultimate++这个IDE的话,以上都不必要,CTRL+F5会在程序结束后提醒程序已经结束,由你在检查运行结果后主动关闭窗口
孩皮妞野 2014-01-20
  • 打赏
  • 举报
回复
程序结束后窗口就关闭了。 通常用的有几个办法: 1. 在控制台执行你的程序; 2. 在main函数结束的大括号}初设置一个断点; 3. 在main结束前加一句system("pause");, 如果你是在windows上练习的话(我估计是);

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧