C++ 为什么我的输出总是0.00? //立方码的输出始终为0.00 #包括 使用名称空间std; 等级公路 { 公众: 空隙设置为道路宽度(双倍宽度); 空套长度(双倍长度); 空隙设置深度(双深度); 需要双层沥青_(); 私人: 双车道深度; 双路宽; 双程长度; 双层道路沥青; }; int main() { 道路宽度、长度、深度、沥青、产量; 双输入宽度=0.0,输入长度=0.0,输入深度=0.0; cout>输入宽度; cout输入长度; cout输入深度; 除了构造输出并打印它的必需()。这就是为什么它打印为零:您从未以任何方式修改输出

C++ 为什么我的输出总是0.00? //立方码的输出始终为0.00 #包括 使用名称空间std; 等级公路 { 公众: 空隙设置为道路宽度(双倍宽度); 空套长度(双倍长度); 空隙设置深度(双深度); 需要双层沥青_(); 私人: 双车道深度; 双路宽; 双程长度; 双层道路沥青; }; int main() { 道路宽度、长度、深度、沥青、产量; 双输入宽度=0.0,输入长度=0.0,输入深度=0.0; cout>输入宽度; cout输入长度; cout输入深度; 除了构造输出并打印它的必需()。这就是为什么它打印为零:您从未以任何方式修改输出,c++,function,class,C++,Function,Class,除此之外,您可能还想知道为什么要构造这么多Road实例。看起来应该只有一个。目前,您有五个Roads:三个是一维的,两个是零维的。除了构造并打印外,您从未对输出做过任何事情_required()。这就是它打印零的原因:您从未以任何方式实际修改输出 除此之外,您可能还想知道为什么要构建这么多的道路实例。看起来应该只有一个。目前,您有五条道路s:三条是一维的,两条是零维的。您只需要一条道路: // The output for cubic yards is always 0.00 #include

除此之外,您可能还想知道为什么要构造这么多
Road
实例。看起来应该只有一个。目前,您有五个
Road
s:三个是一维的,两个是零维的。

除了构造并打印
外,您从未对
输出做过任何事情_required()
。这就是它打印零的原因:您从未以任何方式实际修改
输出


除此之外,您可能还想知道为什么要构建这么多的
道路
实例。看起来应该只有一个。目前,您有五条
道路
s:三条是一维的,两条是零维的。

您只需要一条
道路

// 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);
}
intmain()
{
道路;;
双输入宽度=0.0,输入长度=0.0,输入深度=0.0;
cout>输入宽度;
cout输入长度;
cout输入深度;

你只需要一条路就行了吗

// 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);
}
intmain()
{
道路;;
双输入宽度=0.0,输入长度=0.0,输入深度=0.0;
cout>输入宽度;
cout输入长度;
cout输入深度;

cout问题似乎在这里:

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

问题似乎在这里:

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

道路宽度、长度、深度、沥青、输出;
这对我来说没有意义。您有5条道路,分别称为
宽度
长度
深度
沥青
输出
?我假设您只打算有一条具有不同属性的道路。输出总是
0.0
,因为您有一条道路将其指定为该值,您不会在任何地方更改它。您应该使用调试器查看执行情况,您就会看到问题。我修复了Road类。我创建了一个Road对象Road,它可以工作。感谢所有的响应。
Road width、length、depth、沥青、output;
这对我来说没有意义。您有5条Road c所有的
宽度
长度
深度
沥青
输出
?我假设您的意思是只有一条具有不同属性的道路。输出始终是
0.0
,因为您已将其指定给该值,并且不会在任何地方更改它。您应该使用deb来查看执行情况ugger,你会看到问题所在。我修复了Road类。我制作了一个Road对象,Road,它很有效。感谢所有的回复。