作者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