Program not recognizing if statements

For some reason this program isn't recognizing the if statements I put in setPricePerYd...I can't figure out why that is. Any help would be great!

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
// 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:
		
		void setType(string t)
		{ type = t; }

		void setPricePerYd()
		{
			if (type == "woven")
				pricePerSqYd = 12.99;
			else if (type == "tufted")
				pricePerSqYd = 20.99;
			else if (type == "needlepoint")
				pricePerSqYd = 7.99;
			else 
				cout << "Please enter another value.";
				cin >> type;
		}

		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;
}
closed account (o1vk4iN6)
Tabs have no effect on if statements unlike other languages. You can only use it for one line without brackets.

1
2
3
4
5
6
7
8
9
10
11
if (type == "woven")
	pricePerSqYd = 12.99;
else if (type == "tufted")
	pricePerSqYd = 20.99;
else if (type == "needlepoint")
	pricePerSqYd = 7.99;
else 
	cout << "Please enter another value.";

cin >> type; // this is executed no matter what


Something else to watch out for is dangling else statement:

1
2
3
4
5
if <condition>
     if <condition>
         <expr>
else // this else belongs to the closest if statement, not as it appear in formatting
    <expr>
Topic archived. No new replies allowed.