微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在C ++中三角形区域显示零吗?为什么?

如何解决在C ++中三角形区域显示零吗?为什么?

三角形的区域在输出显示为零,为什么呢?
我做错了什么??

#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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。