问题描述
#include <iostream>
using namespace std;
int main() {
int Base,Height,Area;
// >>> Is anything wrong with the formula??
Area = (0.5) * Height * Base;
cout << "To find the Area of Triangle" << endl << endl;
// Base
cout << "Enter Base length:";
cin >> Base;
cout << endl;
// Height
cout << "Enter Height length";
cin >> Height;
cout << endl << endl;
cout << "Your Base Length is:" << Base << endl;
cout << "Your Height Length is:" << Height << endl;
// calculating area of triangle
// >>> This is the part output is zero
cout << "Area of the triangle is :" << Area << endl;
}
解决方法
当您具有计算值时,必须 对涉及的值进行任何更改来进行计算。与代数x = y * 2
的意思是“ x
的定义是y
的两倍”不同,在C ++中,代数的意思是“将x
的值y
乘以{{ 1}} 现在如此”,并且与未来无关。
对于计算出的东西,您可以使用函数:
2
现在您可以在哪里调用它:
int Area(const int Height,const int Base) {
return 0.5 * Height * Base;
}