// 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?
'道路宽度,长度,深度,沥青,输出;'这对我没有意义。你有5条道路叫做“宽度”,“长度”,“深度”,“沥青”和“输出”?我假设你只想拥有一条具有不同属性的道路。 – Matthew 2015-02-06 05:24:57
输出始终为'0.0',因为您已将其分配给该值,并且不会在任何地方更改。您应该使用调试器查看执行情况,并且您会看到问题。 – 2015-02-06 05:27:49
我修好了Road类。我做了一个道路对象,道路,它工作。感谢所有的答复。 – Tapan 2015-02-06 07:56:52