C++ OOP not storing and displaying content correctly -
i trying create program takes input user length, width, , height of box, , sends appropriate member function , returns main. length, width, , height not storing , displaying correctly in oop using visual studio. code seems take last input (which height) , set length, , set rest 1.0. can't figure out im doing wrong here.
my giftwrap.h file
#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 my giftwrap.cpp file:
#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; } } my giftwrapapp.cpp file:
#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 length of box:" << endl; cin >> len; while (!sallys.setlength(len)){ cout << "invalid selection, try again" << endl; cin >> len; } cout << "please enter width of box:" << endl; cin >> wid; while (!sallys.setlength(wid)){ cout << "invalid selection, try again" << endl; cin >> wid; } cout << "please enter height of 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 using 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; } here result im getting: http://s10.postimg.org/q97jhgc4p/11111.png
in giftwrapapp.cpp inside do while() loop, there 2 mistakes:
cout << "please enter width of box:" << endl; cin >> wid; while (!sallys.setlength(wid)){ // <----- should sallys.setwidthd()!!! cout << "invalid selection, try again" << endl; cin >> wid; } cout << "please enter height of box:" << endl; cin >> hei; while (!sallys.setlength(hei)){ // <-- should sallys.sethight(hei)!! cout << "invalid selection, try again" << endl; cin >> hei; } hope helps!
Comments
Post a Comment