我遇到了这个课程的问题,运费是每磅0.77美元,尺寸总和不能超过150个单位,包裹重量不能超过80磅。一切似乎都有效,除了价格,每次都返回0,无论重量如何。
class Box {
private:
int h , wid , l , tots;
double wei , p;
public :
Box ( int height , int width ,int length , double weight ){
h = height ;
wid = width ;
l = length ;
wei = weight ;
};
Box () {
h = 0;
wid = 0 ;
l = 0;
};
wei = 0;
p=0;
tots=0;
void seth (int height) {
h = height;
};
void setwid (int width) {
wid = width ;
};
void setl (int length) {
l = length;
};
void setwei (double weight) {
wei = weight;
};
void settots (int totalsize) {
tots = totalsize;
};
int getarea (){
int answer;
answer = (h + wid + l);
answer = tots;
return tots;
}
void getdata (){
cout << "Please enter the height of the box: " << endl;
cin >> h ;
cout << "please enter the width of the box: " << endl;
cin >> wid ;
cout << "please enter the length of the box: " << endl;
cin >> l ;
cout << "please enter the weight of the box: " << endl;
cin >> wei ;
};
void findlimit () {
if (((h + wid + l) > 150) && (wei > 80)) {
cout << "box is too large and too heavy" <<endl;
}
else {
if (h + wid + l > 150) {
cout << "box is too large" <<endl;
}
else {
if (wei > 80){
cout << "box is too heavy" <<endl;
}
else {
cout << "box is OK" << endl;
}
}
}
};
void printcost(){
cout << "shipping will cost: $" << p << endl;
};
};
任何帮助将不胜感激
答案 0 :(得分:1)
你永远不会计算价格p
,所以它不是很奇怪它是0.添加计算到打印输出:
void printcost(){
cout << "shipping will cost: $" << (0.77 * p.wei) << endl;
};
或类似的东西。
同样在第二个构造函数中,一些变量放在结束括号之后:
Box () {
h = 0;
wid = 0 ;
l = 0;
};
wei = 0;
p=0;
tots=0;
移动构造函数块中的最后三个变量。