为什么我的输出总是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 < <“沥青需要:”< < output.asphalt_required()< <“cubic yard(s)”< < endl;现在我只写了,因为Stack Overflow说我需要添加更多的上下文,也许是为了平衡代码。为什么我的输出总是0.00?
答
你只需要一个单一的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;
}
答
除了构建它并打印其asphalt_required()
以外,您绝不会对output
做任何事情。这就是为什么它打印零:你从来没有以任何方式实际修改output
!
除此之外,您可能想要考虑为什么要构建如此多的Road
实例。似乎应该只有一个。现在,您有五个Road
s:三个是一维的,两个是零维的。
答
看来,问题就在这里:
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...
'道路宽度,长度,深度,沥青,输出;'这对我没有意义。你有5条道路叫做“宽度”,“长度”,“深度”,“沥青”和“输出”?我假设你只想拥有一条具有不同属性的道路。 – Matthew 2015-02-06 05:24:57
输出始终为'0.0',因为您已将其分配给该值,并且不会在任何地方更改。您应该使用调试器查看执行情况,并且您会看到问题。 – 2015-02-06 05:27:49
我修好了Road类。我做了一个道路对象,道路,它工作。感谢所有的答复。 – Tapan 2015-02-06 07:56:52