C ++ OOP没有正确存储和显示内容

时间:2015-05-30 22:00:32

标签: c++ oop

我正在尝试创建一个程序,从用户那里获取有关框的长度,宽度和高度的输入,并将其发送到相应的成员函数并将其返回到main。我的长度,宽度和高度没有使用Visual Studio在我的OOP中正确存储和显示。代码似乎采用我的最后一个输入(即高度)并将其设置为Length,并将其余部分设置为1.0。我无法弄清楚我在这里做错了什么。

我的GiftWrap.h文件

#ifndef GIFTWRAP_H
#define GIFTWRAP_H
using namespace std;

class GiftWrap{
private:
    double length;
    double width;
    double height;
    double taxRate;
    double pricePerInch;
    double subTotal;
    double total;
    double tax;

public:
    GiftWrap();
    GiftWrap(double, double);
    bool setLength(double);
    bool setWidth(double);
    bool setHeight(double);
    bool setTaxRate(double);
    bool setPricePerInch(double);
    double getLength() const;
    double getWidth() const;
    double getHeight() const;
    double getPriceperInch() const;
    double getTaxRate() const;
    double calcSubTotal();
    double calcTax();
    double calcTotal();
};

#endif

我的GiftWrap.cpp文件:

#include "GiftWrap.h"
#include <iostream>
using namespace std;

GiftWrap::GiftWrap(){
    length = 1.0;
    height = 1.0;
    width = 1.0;
    pricePerInch = 0.0036;
    taxRate = 0.08;
}

GiftWrap::GiftWrap(double r, double c){
    length = 1.0;
    height = 1.0;
    width = 1.0;
    setPricePerInch(r);
    setTaxRate(c);
}


double GiftWrap::getHeight() const{
    return height;
}

double GiftWrap::getWidth() const{
    return width;
}

double GiftWrap::getLength() const{
    return length;
}
double GiftWrap::getPriceperInch() const{
    return pricePerInch;
}

double GiftWrap::getTaxRate() const{
    return taxRate;
}

double GiftWrap::calcSubTotal(){

    subTotal = pricePerInch * ((2 * length * width) + (2 * length * height) + (2 * width * height));
    return subTotal;
}

double GiftWrap::calcTax() {
    tax = subTotal * taxRate;
    return tax;
}

double GiftWrap::calcTotal() {
    total = tax + subTotal;
    return total;
}

bool GiftWrap::setHeight(double h){
    if (h > 0){
        height = h;
        return true;
    }
    else{
        return false;
    }
}

bool GiftWrap::setWidth(double w){
    if (w > 0){
        width = w;
        return true;
    }
    else{
        return false;
    }

}

bool GiftWrap::setLength(double l){
    if (l > 0){
        length = l;
        return true;
    }
    else{
        return false;
    }

}
bool GiftWrap::setTaxRate(double t){
    if (t > 0 && t < 1){
        taxRate = t;
        return true;
    }
    else{
        return false;
    }

}

bool GiftWrap::setPricePerInch(double p){
    if (p > 0){
        pricePerInch = p;
        return true;
    }
    else{
        return false;
    }
}

我的GiftWrapApp.cpp文件:

#include "GiftWrap.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void showInvoice(GiftWrap&);

int main(){
    char selection;
    double len;
    double wid;
    double hei;
    string storeName = "Sallys Gifts";
    GiftWrap sallys(0.0025, 0.925);
    do{
        cout << "GIFT WRAP INVOICE GENERATOR" << endl
            << "------------------------------" << endl
            << "a)Generate Gift Wrap Invoice" << endl
            << "q)Quit" << endl;
        cin >> selection;
        if (selection == 'a' || selection == 'A'){
            cout << "Please enter the length of your box:" << endl;
            cin >> len;
            while (!sallys.setLength(len)){
                cout << "Invalid Selection, try again" << endl;
                cin >> len;
            }
            cout << "Please enter the width of your box:" << endl;
            cin >> wid;
            while (!sallys.setLength(wid)){
                cout << "Invalid Selection, try again" << endl;
                cin >> wid;
            }
            cout << "Please enter the height of your box:" << endl;
            cin >> hei;
            while (!sallys.setLength(hei)){
                cout << "Invalid Selection, try again" << endl;
                cin >> hei;
            }
            cout << "\nGIFT WRAP INVOICE - " << storeName << endl
                << "----------------------------------" << endl;
            showInvoice(sallys);
        }
        else if (selection == 'q' || selection == 'Q'){
            cout << "Thank you for using this program!" << endl;
        }
        else{
            cout << "Invalid Selection, try again" << endl;
        }

    } while (selection != 'q' && selection != 'Q');
    system("PAUSE");
    return 0;
}

void showInvoice(GiftWrap& r){
    cout << "Box Length: " << fixed << setprecision(2)  << r.getLength() << endl;
    cout << "Box width: " << fixed << setprecision(2) << r.getWidth() << endl;
    cout << "Box Height: " << fixed << setprecision(2) << r.getHeight() << endl;
    cout << "Price Per Inch: " << fixed << setprecision(4) << r.getPriceperInch() << "\n" << endl;

    cout << "Subtotal: " << fixed << setprecision(2) << r.calcSubTotal() << endl;
    cout << "Tax: " << fixed << setprecision(2) << r.calcTax() << endl;
    cout << setw(5) << "----------" << endl;
    cout << "TOTAL: " <<  fixed << setprecision(2) << r.calcTotal() << endl;
    cout << endl;

}

这是我得到的结果: http://s10.postimg.org/q97jhgc4p/11111.png

1 个答案:

答案 0 :(得分:3)

GiftWrapApp.cpp循环内的do while()中,有两个错误:

cout << "Please enter the width of your box:" << endl;
cin >> wid;
while (!sallys.setLength(wid)){ // <----- Should be sallys.setWidthd()!!!
    cout << "Invalid Selection, try again" << endl;
    cin >> wid;
}
cout << "Please enter the height of your box:" << endl;
cin >> hei;
while (!sallys.setLength(hei)){ // <-- Should be sallys.setHight(hei)!! 
    cout << "Invalid Selection, try again" << endl;
    cin >> hei;
}

希望这有帮助!