2015-05-30 4 views
0

Я пытаюсь создать программу, которая вводит от пользователя информацию о длине, ширине и высоте поля и отправляет ее соответствующей функции-члену и возвращает ее обратно в главный. Моя длина, ширина и высота не сохраняются и отображаются правильно в моем ООП с помощью Visual Studio. Код, кажется, берет мой последний вход (который является высотой) и устанавливает его в Length, а остальные - 1.0. Я не могу понять, что я здесь делаю неправильно.C++ OOP не правильно сохраняет и отображает содержимое

Мой 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

Добро пожаловать в переполнение стека. Посмотрите на страницу в [Минимальные полные примеры] (http://stackoverflow.com/help/mcve). Этот код слишком длинный и сложный; если бы вы сократили его до более простого примера, вы, вероятно, заметили бы ошибку самостоятельно, и если бы вы не сделали бы эту задачу намного проще. – Beta

ответ

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; 
} 

Надеется, что это помогает!

+0

Боже, мне нужно впитать кофе. Я мог бы поклясться, что я проверяю свой код, как более 10 раз в основном, и сдался (думая, что это будет в моем конструкторе). Спасибо за вашу помощь. – Radon5K

+0

Добро пожаловать. Случается со всеми. –

+0

Я пытался это сделать раньше, но он сказал, что вам нужно подождать 2 минуты -_-. Спасибо за ваш быстрый ответ. – Radon5K

Смежные вопросы