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

计算器程序,无法正确输出最后的结果!

含笑半步颠 发布于 2007-03-07 22:35, 1098 次点击
// 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编辑过]

12 回复
#2
maoguoqing2007-03-07 23:07

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

#3
含笑半步颠2007-03-08 10:07
只有本站会员才能查看附件,请 登录

显示是这样 我照你给的方法改过来了
#4
虫虫飞ya飞2007-03-08 10:17
首先你程序的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编辑过]

#5
含笑半步颠2007-03-08 20:56

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

#6
nuciewth2007-03-08 21:50
接收换行符,使得不会赋值给op
#7
summerwxf2007-03-08 23:14
嘿嘿
#8
i73662007-03-09 12:50
回复:(含笑半步颠)计算器程序,无法正确输出最后的...

// 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吗,我觉得应该用小写的,这更符合习惯.

#9
落枫0433352007-03-09 23:45
回复:(含笑半步颠)计算器程序,无法正确输出最后的...
我用你贴得代码在我机器上运行很好啊!!没有什么问题。。。我用的是VC6.0
#10
lehmann2007-03-11 19:08
VC6.0 没问题
#11
冰天雪2007-03-11 19:43
这个是不是编译器的问题啊?
#12
含笑半步颠2007-03-11 21:05
不知道啦~ 我也是用VC6.0
老是没结果
#13
woailiwei2007-03-13 10:10
可以运行VC6.0
1