Program all around messed up. Tell me what I'm doing wrong?

This is an assignment for class. I have everything right up to the point where the total and savings (if any) part came up. I only want certain messages to come up if certain hours are put in. For some reason it goes straight to the first option and outputs that message.

As you can see, I put that section as if, then if else's. I tried using only if and things were a lot worse, showing multiple unwanted messages and different total.

I would really appreciate help with this. I'm completely stumped.

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
if (pack == 'b' || pack == 'B' && hours > 25)
		{
			over = hours - 20;
			total = total + over;

			cout<<setprecision(2)<<fixed;
			cout<<"The total amount due is: $"<<total<<endl;
			cout<<"You could have saved $"<<over*2<<" by selecting package C."<<endl;
		}

		else if (pack == 'b' || pack == 'B' && hours > 20 && hours < 25)
		{
			over = hours - 20;
			total = total + over;

			cout<<setprecision(2)<<fixed;
			cout<<"The total amount due is: $"<<total<<endl;
		}


		else if (pack == 'b' || pack == 'B' && hours <= 20)
		{
			cout<<setprecision(2)<<fixed;
			cout<<"The total amount due is: $"<<total<<endl;
		}



		if (pack == 'a' || pack == 'A' && hours > 15)
		{
			over = hours - 10;
			total = total + (over * 2);

			cout<<setprecision(2)<<fixed;
			cout<<"The total amount due is: $"<<total<<endl;
			cout<<"You could have saved $"<<over<<" by selecting package B."<<endl;
			cout<<"You could have saved $"<<over*2<<" by selecting package C."<<endl;
		}

		else if (pack == 'a' || pack == 'A' && hours == 14)
		{
			over = hours - 10;
			total = total + (over * 2);

			cout<<setprecision(2)<<fixed;
			cout<<"The total amount due is: $"<<total<<endl;
			cout<<"You could have saved $"<<over<<" by selecting package B."<<endl;
		}	

		else if (pack == 'a' || pack == 'A' && hours > 10 && hours < 13)
		{
			over = hours - 10;
			total = total + (over * 2);

			cout<<setprecision(2)<<fixed;
			cout<<"The total amount due is: $"<<total<<endl;
		}

		else if (pack == 'a' || pack == 'A' && hours <= 10)
		{
			cout<<setprecision(2)<<fixed;
			cout<<"The total amount due is: $"<<total<<endl;
		}
	}

	system ("pause");
	return 0;
}
I just would like to point out, that when you use OR and AND operators in a single expression, use brackets to sort priorities.

 
		else if ((pack == 'b' || pack == 'B') && hours > 20 && hours < 25)


The way you do it could easily get you wrong results.

Try using brackets on all your || operators, and tell me whether it works.
Last edited on
can you put output?
And I think you can put cout<<setprecision(2)<<fixed; in first line, and delete it in other lines.

in
you do not check if hours == 25,
you do not check if hours == 15,
you do not check if hours == 13,

can't see other problems.
Sorry for the delayed reply.

Destroyer, that completely worked. I'm very grateful for the tip. I'll hold onto that for the future.

Shinigami, do you mean I don't have to continue typing that out in each 'if'?
You're welcome. Mark the post as solved :)
Shinigami, do you mean I don't have to continue typing that out in each 'if'?

if you are toking about setprecision(2), then yes. Look to my example.
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double number = 12.32656;
	cout << number << endl;
	cout << setprecision(2) << fixed;
	cout << number << endl;

	int cc;
	do
	{
		cout << "Enter number from 1 to 4: ";
		cin >> cc;

		if (cc == 1)
		{
			cout << 45.2355654;
		}
		else if (cc == 2)
		{
			cout << 78.32654;
		}
		else if (cc == 3)
		{
			//if you enter 3 precision will be 3.
			cout << setprecision(3) << fixed;
			cout << 12.365654;
		}
		else if (cc == 4)
		{
			cout << 6.245756;
		}
		cout << endl;
	}
	while (cc > 0);
	
	return 0;
}


output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
12.3266
12.33
Enter number from 1 to 4: 1
45.24
Enter number from 1 to 4: 2
78.33
Enter number from 1 to 4: 4
6.25
Enter number from 1 to 4: 3
12.366
Enter number from 1 to 4: 1
45.236
Enter number from 1 to 4: 2
78.327
Enter number from 1 to 4: 4
6.246
Enter number from 1 to 4:
Last edited on
Topic archived. No new replies allowed.