Help with why program isn't working correctly
Apr 24, 2012 at 2:43am UTC
Now that I have fixed the strings...why isn't the program figuring out the math properly? I think that it is not calling the pricePerSqYd correctly, but i'm not sure how to make it do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// This program nests one class inside another. It has a class
// with a member variable that is an instance of another class.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Rectangle
{
private :
double length;
double width;
public :
void setLength(double len)
{ length = len; }
void setWidth(double wid)
{ width = wid; }
double getLength()
{ return width; }
double getArea()
{ return length * width; }
};
class Carpet
{
private :
double pricePerSqYd;
string type;
string t;
Rectangle size; // Size is an instance of the Rectangle class
public :
string setType(string type)
{ return type; }
void setPricePerYd(double p)
{
if (type == "woven" )
p = 12.99;
else if (type == "tufted" )
p = 20.99;
else if (type == "needlepoint" )
p = 7.99;
else
cout << "Please enter another value." ;
cin >> type;
pricePerSqYd = p;
}
void setDimensions(double len, double wid)
{ size.setLength(len/3); // Convert feet to yards
size.setWidth(wid/3);
}
double getTotalPrice()
{ return (size.getArea() * pricePerSqYd); }
};
// ************************* Client Program ********************************
int main()
{
Carpet purchase; // This variable is a Carpet object
double pricePerYd;
double length;
double width;
string type;
cout << "Room length in feet: " ;
cin >> length;
cout << "Room width in feet : " ;
cin >> width;
cout << "Type of carpet (woven, tufted, or needlepoint): " ;
cin >> type;
purchase.setDimensions(length, width);
purchase.setType(type);
cout << "\nThe total price of my new " << length << " x " << width << " " << type
<< " carpet is $" << purchase.getTotalPrice() << endl;
system("pause" );
return 0;
}
Last edited on Apr 24, 2012 at 3:16am UTC
Apr 24, 2012 at 2:47am UTC
It's simply if (type == "woven" )
, no special functions.
Apr 24, 2012 at 2:54am UTC
Oh, thank you!
Topic archived. No new replies allowed.