if-else + switch

I am enrolled in a c++ course. I was given a lab today after having a lesson on switch statements, which is great because we were told to not use switch for the validation in the lab. So, I pounded my way though this code and it is killing me. I can't seem to get the program to validate whether the package is acceptable. Thanks in advance.

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
#include <iostream>
using namespace std;

void main()
{
	char p;
	int h;
	double i;

	cout<<"Enter Package: ";
	cin>>p;
	cout<<"Enter Hours: ";
	cin>>h;

	if ( p!='a' || p!='A' || p!='b' || p!='B' || p!='c' || p!='C' )
	{
		cout<<"Invalid Package"<<endl;
	}
	else if (h<0 || h>720)
	{
		cout<<"Invalid Hours"<<endl;
	}
	else
	{
		switch (p)
		{
			case 'a':
			i=15+(h-50)*2;
			break;
			case 'A':
			i=15+(h-50)*2;
			break;
			case 'b':
			i=20+(h-100)*1.5;
			break;
			case 'B':
			i=20+(h-100)*1.5;
			break;
			case 'c':
			i=25+(h-150)*1;
			break;
			case 'C':
			i=25+(h-150)*1;
			break;
		}
	}
	cout<<"Bill: "<<i<<endl;
}
You can't use the || (that means OR) operator in the first if, because it's impossible to the variable p be equal to all those letters at the same time. Try using the && (that means AND) operator instead.
yes, indeed. you have to use xor
Thanks a lot. I was under the impression that I was checking to see that " p does not equal 'a' or 'A' or 'b'...etc." Solution worked great, tulicloure. Adding my looping statements now. Did the rest of the code look ok?
You could have just used default.
What are you referring to, helios?
If the switch statement contains a default label, it jumps to it if none of the cases match.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int a=0;
switch (a){
    case 1:
        //...
        break;
    case 2:
        //...
        break;
    case 3:
        //...
        break;
    default:
        //(Will always pass though here)
        //...
        break;
}
Ah, yes. Howerever, my professor instructed us to use only the if-else construct for validation, and allowed us to use switch for computation.
Topic archived. No new replies allowed.