【问题标题】:Why am I getting garbage value here although i provided argument?尽管我提供了参数,为什么我在这里得到垃圾值?
【发布时间】:2021-11-05 21:23:50
【问题描述】:
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;

class Triangle
{
public:
    int a, b, c;
    void getdata();
    int peri(int a, int b, int c)
    {
        return a + b + c;
    }
    float s;
    float area;
    float Area(int a, int b, int c)
    {
        s = (a + b + c) / 2;
        area = sqrt(s * (s - a) * (s - b) * (s - c));
        cout << area;
    }

};

void Triangle::getdata()
{
    cin >> a >> b >> c;
}

int main()
{
    int x, y, z;
    cout << "Enter the three sides";
    Triangle t1;
    t1.getdata();
    cout << "The area of that triangle is " << t1.Area(x, y, z) << "and the perimeter "
         << t1.peri(x, y, z);
    return 0;
}

实际上,虽然没有编译错误,但这段代码给了我垃圾值。它没有给我想要的面积和周长输出。那么为什么我会得到垃圾值呢?

【问题讨论】:

  • edit您的问题正确缩进您的代码。代码,因为它目前的格式,很难跟上。
  • 当您将输入数据存储在成员变量中时,为什么还要将参数传递给Areaperi?您传递的变量也未初始化。 Area 函数也不返回值。如果你把它们调得足够高,你应该会得到一个编译警告。
  • 你不需要int x,y,z;;你永远不会初始化它们。你不需要int peri(int a,int b,int c),你的三角形有它自己的a,b.c。替换为int peri()Area 也一样。为什么peri 返回值而Area 打印值却什么也不返回?为什么Area 大写而peri 不是?为什么 sarea 是类成员,即使您在单个函数中初始化并使用它们?
  • ` s = (a + b + c) / 2;` 将因整数除法而截断。哎呀。使用0.5f * (a + b + c)
  • @n.1.8e9-where's-my-sharem。是的,我遇到了问题......所以当我将成员变量作为输入时,我不必将参数传递给成员函数?对吗?

标签: c++ class object area


【解决方案1】:

这里你从三角形初始化a, b, c

void Triangle::getdata( )
{
   cin>>a>>b>>c;
}

但是这里你使用了没有初始化的x,y,z,有问题

int main()
{
   int x,y,z; //<-- these three variables are nowhere initialised
   cout<<"Enter the three sides";
   Triangle t1;
   t1.getdata();
   cout<<"The area of that triangle is "
      <<t1.Area( x, y, z) //where x, y, z are initialised??
      <<"and the perimeter " 
      <<t1.peri(x,y,z);  //where x, y, z are initialised?
   return 0;
}

更新 1:正如@Bathsheba s = (a + b + c) / 2; 所指出的,由于整数除法,将截断。哎呀。使用 0.5f * (a + b + c)。 ——

这就是你应该如何实现你的三角形

class Triangle
{
public:
    int a, b, c;
    void getdata();
    int peri() //remove the arguments, the class has its own members to use
    {
        return a + b + c;
    }
    float s;
    float area;
    float Area() //remove the arguments, the class has its own members to use
    {
        s = 0.5f * (a + b + c); //As pointed by @Bathsheba
        area = sqrt(s * (s - a) * (s - b) * (s - c));
        cout << area;
    }

};

然后这样写main函数

int main()
{
   //no additional x, y, z
   cout<<"Enter the three sides";
   Triangle t1;
   t1.getdata();
   cout<<"The area of that triangle is "
      <<t1.Area() //no arguments
      <<"and the perimeter " 
      <<t1.peri();  //no arguments
   return 0;
}

【讨论】:

  • 如果您指出s = (a + b + c) / 2; 的问题,将支持。请参阅我对这个问题的评论。
  • @armagedescu 非常感谢....是的,它现在正在工作,给了我想要的输出。
  • @Bathsheba ...我对 s=(a+b+c)/2 没有任何问题,因为 s 已经浮动...
  • @ISHAGHUGE 根据我的经验,我以前也遇到过这样的问题。现在编译器更加智能。但是在旧版本的编译器中,如果第一个操作数是 int,那么很可能整个运算结果都是 int,并且只有在赋值操作时才转换为 float。
  • @armagedescu 哦,我明白了......我是编码初学者......所以面临这样的问题......
猜你喜欢
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
相关资源
最近更新 更多