| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1093 人关注过本帖
标题:计算器程序,无法正确输出最后的结果!
只看楼主 加入收藏
含笑半步颠
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-2-27
收藏
 问题点数:0 回复次数:12 
计算器程序,无法正确输出最后的结果!
// c++ code template
#include <iostream.h>
void main()
{
float num1;
float num2;
char op;
float ans;
cout << "Please enter a number: " ;
cin >> num1;
cout << "please entet another nunber: " ;
cin >> num2;
cout << "press A to add the two numbers."
<< endl
<< "press S to subtract the two numbers."
<< endl
<< "press M to multiply the two numbers."
<< endl
<< "press D to divide the two numbers."
<< endl;
cin.ignore();
cin >> op;
if (op == 65)
{
ans = num1 + num2;
cout << "The answet is " << ans << endl;
}
if (op == 83)
{
ans = num1 - num2;
cout << "The answet is " << ans << endl;
}
if (op == 77)
{
ans = num1 * num2;
cout << "The answet is " << ans << endl;
}
if (op == 68)
{
ans = num1 / num2;
cout << "The answet is: " << ans << endl;
}
if (op != 65 && op !=83 && op != 77 && op != 68)
{
cout << "No valid operation was chosen!" << endl;
}
}
没有输出最后的结果!?

[此贴子已经被作者于2007-3-8 10:09:03编辑过]

搜索更多相关主题的帖子: 计算器 结果 输出 
2007-03-07 22:35
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 

if (op != 65 && op !=83 && op != 77 && op != 68)
如果还是不行的话你在 cin >> op;之前加个 cin.ignore();试试


天行健,君子以自强不息!!QQ:68660681
2007-03-07 23:07
含笑半步颠
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-2-27
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

显示是这样 我照你给的方法改过来了
2007-03-08 10:07
虫虫飞ya飞
Rank: 1
等 级:新手上路
帖 子:122
专家分:0
注 册:2005-11-28
收藏
得分:0 
首先你程序的op那是字符型的你做if判断不能按你的意思正确判断,把op改成int型.另外你是不是没看程序只是复制粘贴的,你输入23,和输入123后下面是正确的输出啊你程序就是这么设定的,你想输出计算结果后面还要继续输入op的值才能判断你要用哪个输入符.
#include <iostream.h>
void main()
{
float num1;
float num2;
int op;
float ans;
cout << "Please enter a number: " ;
cin >> num1;
cout << "please entet another nunber: " ;
cin >> num2;
cout << "press A to add the two numbers."
<< endl
<< "press S to subtract the two numbers."
<< endl
<< "press M to multiply the two numbers."
<< endl
<< "press D to divide the two numbers."
<< endl;
cin >> op;
if (op == 65)
{
ans = num1 + num2;
cout << "The answet is " << ans << endl;
}
if (op == 83)
{
ans = num1 - num2;
cout << "The answet is " << ans << endl;
}
if (op == 77)
{
ans = num1 * num2;
cout << "The answet is " << ans << endl;
}
if (op == 68)
{
ans = num1 / num2;
cout << "The answet is: " << ans << endl;
}
if (op != 65 && op !=83 && op != 77 && op != 68)
{
cout << "No valid operation was chosen!" << endl;
}
}

[此贴子已经被作者于2007-3-8 10:22:16编辑过]


2007-03-08 10:17
含笑半步颠
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-2-27
收藏
得分:0 

哦哦~
知道了~ 待研究研究!

2007-03-08 20:56
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
接收换行符,使得不会赋值给op

倚天照海花无数,流水高山心自知。
2007-03-08 21:50
summerwxf
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-3-8
收藏
得分:0 
嘿嘿
2007-03-08 23:14
i7366
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-1-14
收藏
得分:0 
回复:(含笑半步颠)计算器程序,无法正确输出最后的...

// c++ code template
#include <iostream.h>
void main()
{
float num1;
float num2;
char op;
float ans;
cout << "press A to add the two numbers."
<< endl
<< "press S to subtract the two numbers."
<< endl
<< "press M to multiply the two numbers."
<< endl
<< "press D to divide the two numbers."
<< endl;
// cin.ignore();注意这里,如果保留将会有一个输入被忽略,不会的正确的结果.

cout << "Please enter a number: " ;
cin >> num1;
cout << "please entet another nunber: " ;
cin >> num2;
cin >> op;
if (op == 65+32)
{
ans = num1 + num2;
cout << "The answet is " << ans << endl;
}
if (op == 83+32)
{
ans = num1 - num2;
cout << "The answet is " << ans << endl;
}
if (op == 77+32)
{
ans = num1 * num2;
cout << "The answet is " << ans << endl;
}
if (op == 68+32)
{
ans = num1 / num2;
cout << "The answet is: " << ans << endl;
}
if (op != 65 && op !=83 && op != 77 && op != 68)
{
cout << "No valid operation was chosen!" << endl;
}
}
另外,要注意ASCII吗,我觉得应该用小写的,这更符合习惯.

2007-03-09 12:50
落枫043335
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-3-9
收藏
得分:0 
回复:(含笑半步颠)计算器程序,无法正确输出最后的...
我用你贴得代码在我机器上运行很好啊!!没有什么问题。。。我用的是VC6.0
2007-03-09 23:45
lehmann
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-8-15
收藏
得分:0 
VC6.0 没问题
2007-03-11 19:08
快速回复:计算器程序,无法正确输出最后的结果!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019471 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved