這是我的實驗程序。我必須應用兩個公式併爲兩者打印答案。第一個是在這一行:在C++計算中無法獲得正確答案
cout<<"Hypoteneus is: " <<sqrt((x*x)+(y*y)) <<endl;
這一個工作。問題是在這一個:
cout<<"Area is: " <<a*x <<((1/2)*x*y);
的問題是,它打印其答案爲0。我把a*x
進行測試,這工作,但表達(1/2)*x*y
被渲染0作爲答案。我輸入x
爲1和y
爲2.請幫助。
int main()
{
float x, y, a=122;
cout<<"Enter x and y.\n";
cin>>x >>y;
cout<<"Hypoteneus is: " <<sqrt((x*x)+(y*y)) <<endl;
fflush(stdin);
cout<<"Area is: " <<a*x <<((1/2)*x*y);
getch();
return 0;
}
'fflush(stdin)'是直線的未定義行爲。去掉它。 – chris 2015-03-02 18:50:36
也是不必要的。 – nikaltipar 2015-03-02 18:51:21
調試提示:如果您對爲什麼某行沒有給出您所期望的答案感到困惑,請將其分解成小塊並打印以找出錯誤發生的位置:'cout <<(1/2)<<「 ,「<<(1/2)* x <<」,「<<(1/2)* x * y <<」\ n「;' – indiv 2015-03-02 18:53:16