作者yongtw123 (六円)
看板C_and_CPP
标题[问题] int()的奇怪现象
时间Fri Feb 27 01:12:13 2009
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int m, x, y, z;
input:
cout<<"input: ";
cin>>x>>y>>z;
cin.get();
if (x<0)
goto input;
if (y<0)
goto input;
if (z=0)
goto input;
m = int((4.0/7)*(pow(y,1.75)-pow(x,1.75))+(8.0/z));
cout<<m<<endl;
system("pause");
return(0);
}
两个奇怪现象:
1. 输入1 1 8
得1
但是输入2 2 8
得0
(两者应该一样的)
2. 第三个数字输入0
程式并不会跳到input重来
PS: 加cin.get()是因为之後还要加cin.getline()
想要请问是哪里的问题
毫无头绪...
先感谢~
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.135.160.55
1F:→ LPH66:if(z=0) // 好像该用 == ? 02/27 01:12
2F:推 gozha:不会跳回input是因为if(z=0)是永远进不去if(z==0)才可以 02/27 01:29
3F:推 plover:碰到这种东西...多写 if (0==z) 就不会出包了.. 02/27 01:53
4F:→ plover:少打一个= 会变成 0=z, compiler会靠背不会让你过 02/27 01:53
5F:→ yongtw123:噢...原来如此 那只剩第一个问题 谢谢各位~ 02/27 02:17
6F:推 H45:我输入 1 1 8 的结果和 2 2 8 一样都是 1 02/27 10:23
7F:→ H45:(已改成 z==0) 02/27 10:23
8F:→ yongtw123:如果不加int(), m在2 2 8的输入後是1, 但int()後变成0 02/27 12:38
9F:→ smalldulan:我觉得是运算的问题~浮点数和整数运算造成的错误~ 02/27 13:08
10F:→ smalldulan:我把变数型态从int改成double就正常了~要在转换回int 02/27 13:08
11F:→ smalldulan:就加static_cast<int>(运算式)吧 02/27 13:10
12F:推 VictorTom:请问这里用static_cast<int>的作用是?? 02/27 13:14
13F:推 legnaleurc:C++ style 转型 02/27 13:57
14F:推 VictorTom:ㄟ~~我就算用这个转型, 结果好像一样是0啊?? 02/27 13:59