为什么我的输出总是 0.00?

Why is my output always 0.00?

// The output for cubic yards is always 0.00

#include <iostream>
using namespace std;

class Road
{
public:
    void set_road_width(double width);
    void set_road_length(double length);
    void set_road_depth(double depth);
    double asphalt_required();
private:
    double roadDepth;
    double roadWidth;
    double roadLength;
    double roadAsphalt;
};

int main()
{
    Road width, length, depth, asphalt, output;
    double inputWidth = 0.0, inputLength = 0.0, inputDepth = 0.0;


    cout << "Enter the width of the road in miles: ";
    cin  >> inputWidth;
    cout << endl;
    cout << "Enter the length of the road in miles: ";
    cin  >> inputLength;
    cout << endl;
    cout << "Enter the depth of the road in inches: ";
    cin  >> inputDepth;
    cout << endl;


    width.set_road_width(inputWidth);
    length.set_road_length(inputLength);
    depth.set_road_depth(inputDepth);
    asphalt.asphalt_required();


    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);


    cout << "The width of the road is: " << inputWidth << " mile(s)" << endl;
    cout << "The length of the road is: " << inputLength << " mile(s)" << endl;
    cout << "The depth of the road is: " << inputDepth << " inch(es)" << endl;  
    cout << "Asphalt required: " << output.asphalt_required() << " cubic yard(s)" << endl;

    return 0;
}

void Road::set_road_width(double width)
{
    roadWidth = width;
}

void Road::set_road_length(double length)
{
    roadLength = length;
}

void Road::set_road_depth(double depth)
{
    roadDepth = depth;
}

double Road::asphalt_required()
{
    double widthIntoYards = 0.0, lengthIntoYards = 0.0, depthIntoYards = 0.0, yardConversionFactor = 0.333;

    widthIntoYards = ((roadWidth * 5280.00) * yardConversionFactor);
    lengthIntoYards = ((roadLength * 5280.00) * yardConversionFactor);
    depthIntoYards = ((roadDepth / 12.00) * yardConversionFactor);

    roadAsphalt = (widthIntoYards * lengthIntoYards * depthIntoYards);

    return(roadAsphalt);
}

输出始终为 0.00。我认为它与 asphalt_required() 函数或 cout << "Asphalt required: " << output.asphalt_required() << "cubic yard(s)" << endl; 行有关。现在我只是在写,因为 Stack Overflow 说我需要添加更多上下文,也许是为了平衡代码。

除了构造它并打印它的 asphalt_required() 之外,您永远不会对 output 做任何事情。这就是它打印零的原因:您实际上从未以任何方式修改 output

除此之外,您可能要考虑为什么要构造这么多 Road 个实例。好像应该只有一个。就目前而言,您有五个 Road:三个是一维的,两个是零维的。

你只需要一张road:

int main()
{
    Road road;
    double inputWidth = 0.0, inputLength = 0.0, inputDepth = 0.0;


    cout << "Enter the width of the road in miles: ";
    cin  >> inputWidth;
    cout << endl;
    cout << "Enter the length of the road in miles: ";
    cin  >> inputLength;
    cout << endl;
    cout << "Enter the depth of the road in inches: ";
    cin  >> inputDepth;
    cout << endl;


    road.set_road_width(inputWidth);
    road.set_road_length(inputLength);
    road.set_road_depth(inputDepth);
    road.asphalt_required();


    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);


    cout << "The width of the road is: " << inputWidth << " mile(s)" << endl;
    cout << "The length of the road is: " << inputLength << " mile(s)" << endl;
    cout << "The depth of the road is: " << inputDepth << " inch(es)" << endl;
    cout << "Asphalt required: " << road.asphalt_required() << " cubic yard(s)" << endl;

    return 0;
}

看来问题出在这里:

Road width, length, depth, asphalt, output;

/// Some code

width.set_road_width(inputWidth);
length.set_road_length(inputLength);
depth.set_road_depth(inputDepth);
asphalt.asphalt_required();

以及它是怎样的

Road output;

/// Some code

output.set_road_width(inputWidth);
output.set_road_length(inputLength);
output.set_road_depth(inputDepth);
output.asphalt_required();

这对你有帮助。

这是因为您创建了一个 class 的实例,其中您已经拥有用于计算的所有内容。在这种情况下,最好在构造函数中包含所需的一切,如下所示:

class Road
{
public:
   Road(double width, double length, double depth)
   double asphalt_required();
private:
   double roadDepth;
   double roadWidth;
   double roadLength;
   double roadAsphalt;
};

Road::Road(double width, double length, double depth)
 : roadDepth(depth)
 , roadWidth(width)
 , roadLength(length) {
}

/// Other code...

这样更好,因为它还可以防止您出现此类错误。您需要做的就是做一些修改

double inputWidth = 0.0, inputLength = 0.0, inputDepth = 0.0;

/// Here we got all needed values...

Road output(inputWidth, inputLength, inputDepth);
output.asphalt_required();

/// And then output...