为什么我的输出总是0.00?

时间:2015-02-06 05:22:39

标签: c++ function class

// 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&lt;&lt;有关。 “需要沥青:”&lt;&lt; output.asphalt_required()&lt;&lt; “立方码”&lt;&lt; ENDL;现在我只是写作,因为Stack Overflow说我需要添加更多的上下文,或许是为了平衡代码。

3 个答案:

答案 0 :(得分:3)

问题似乎在这里:

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 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...

答案 1 :(得分:1)

除了构建output之外,你永远不会做任何事情并打印asphalt_required()。这就是为什么它打印为零:你从来没有以任何方式实际修改output

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

答案 2 :(得分:1)

您只需要一个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;
}