If variable != specified character?

I've tried this several different ways, and just can't seem to get it to work. This is the section I'm working with:

1
2
3
4
5
6
7
8
9
10
		if ( (package == 'Q') || (package == 'q' ) )
		{
			cout << "Thank you for using this program. Goodbye." << endl;
			return 0;
		}
		else if ( (package != 'a') || (package != 'A') )
		{
			cout << "Enter only A, B, C, or Q.\n" << endl;
			continue;
		}


The first part works -- if I enter a 'Q' or 'q' the program displays that line and terminates. I'm trying to get the second part to start the program over if PACKAGE does not equal A, a, B, b, C, or c. (I have just A and a for testing purposes at this time). Thanks for any assistance.
well, u can simply do it with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if ( (package == 'Q') || (package == 'q' ) )
		{
			cout << "Thank you for using this program. Goodbye." << endl;
			return 0;
		}
		else if ( (package == 'a') || (package == 'A') )
		{
			//things with a
			continue;
		}
                else if ( (package == 'b') || (package == 'B') )
		{
			//things with B
			continue;
		}
                // etc
                else
                {
                        cout << "Enter only A, B, C, or Q.\n" << endl;
			continue;
                }
Last edited on
Well I have to use if...else AND switch statements in this assignment, so I have the A,B,C with the switch statements...
Think about it.
1
2
3
4
5
6
Character      Different from 'a'      Different from 'A'
'x'            1                       1
'X'            1                       1
'B'            1                       1
'a'            0                       1
'A'            1                       0

( x != 'a' || x != 'A' ) is equivalent to ( !( x == 'a' && x == 'A' ) )
Is there any character that is equal to both 'a' and 'A' at the same time? Can you figure out what needs to be changed to make the condition make sense?
Last edited on
you can switch only numbers, so u need to use it like I did
( x != 'a' || x != 'A' ) is equivalent to ( x == 'a' && x == 'A' )


It's totally not, dude.

If x is set to 'a', ( x != 'a' || x != 'A' ) comes out as true because x != 'A', but ( x == 'a' && x == 'A' ) comes out as false.
Whoops. I forgot the outer negation. I'll fix it now.
closed account (1vRz3TCk)
Well I have to use if...else AND switch statements in this assignment
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
#include <iostream>

using namespace std;

int main()
{
    for(char package = 'a'; package < 'z'; ++package)
    {
        if ( (package == 'Q') || (package == 'q' ) )
        {
            cout << "Thank you for using this program. Goodbye." << endl;
            return 0;
        }
        else
        {
            switch (package)
            {
            case 'a':
            case 'b':
            case 'c': 
                {
                    cout << package << endl;
                }
                continue;
            default:
                {
                    cout << "Enter only A, B, C, or Q." << endl;
                }
            } 
        }
    }
}
Last edited on
Well I have to have two different inputs. Here's everything I have now... I'm not looking for someone to do it for me... I want to know where I'm going wrong and how to fix it. The program is supposed to loop unless Q or q is entered. Right now it terminates if anything other than a,b,c,q is entered, and only after the number of units is entered, but should display the error BEFORE querying for the number of units then looping back to the beginning of the program. Anyway, here it is... and thanks for all of your help. Hopefully I'll get this soon enough.

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

int main()
{ 
	//Sets variables
	char package;
	int units;
	double pricea;
	double priceb;
	double pricec = 39.95;
	
	do
	{
		//Requests input for package
		cout << "Which package are you shopping for? (Enter A, B, C, or Q to quit)?" << endl;
		cin >> package;
		
		
		if ( (package == 'Q') || (package == 'q' ) )
		{
			cout << "Thank you for using this program. Goodbye." << endl;
			return 0;
		}
		

		//Requests input for package and displays error if not between 0-672
		cout << "How many message units?" << endl;
		cin >> units;
		if ((units < 0) || (units > 672))
		{
			cout << "Please enter a number from 0 through 672 for message units.\n" << endl;
			continue;
		}

		//Assigns formulated/set prices depending on number of units for Packages A and B
		if (units > 20)
		{ 
			priceb = ( 19.95 + ( 1 * (units - 20) ) );
		}
		else priceb = 19.95;
		if (units > 10)
		{ 
			pricea = ( 9.95 + ( 2 * (units - 10) ) );
		}
		else pricea = 9.95;
		
		
		switch (package)
		{
			case 'A':
			case 'a':
				cout << "The charges are $" << pricea << endl;
				if (pricea > priceb)
				{
					cout << "By switching to Package B, you would save $" << pricea - priceb << endl;
				}
				if (pricea > pricec)
				{
					cout << "By switching to Package C, you would save $" << pricea - pricec << endl << endl;
				}
				break;
			case 'B':
			case 'b':
				cout << "The charges are $" << priceb << endl;
				if (priceb > pricec)
				{
					cout << "By switching to Package C, you would save $" << priceb - pricec << endl << endl;
				}
				break;
			case 'C':
			case 'c':
				cout << "The charges are $\n" << pricec << endl;
				break;
			case 'Q':
			case 'q':
				cout << "Thank you for using this program. Goodbye." << endl;
				return 0;
				break;
			default:
				cout << "Enter only A, B, C, or Q.\n" << endl;
		}

	}
	while ( (package == 'a') || (package == 'A') );
	return 0;
}
Topic archived. No new replies allowed.