!= operator with ASCII

I'm in an entry level C++ class at school and I was given the task to write a program that involved arrays and such(the finished working code).
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
#include<iostream>
using namespace std;

int main()
{
	const int arraySize = 7;
	char partNum[arraySize];
	int valid = 1;

	cout<<"Enter a part number - first position must be capital A, B, or C."<<endl;
	cout<<"Positions two and three must be numerals that in sequence range between ten and forty-nine."<<endl;
	cout<<"Position four must be a lowercase letter."<<endl;
	cout<<"Positions five and six must be numerals that in sequence range between twenty-two and sixty-six."<<endl;

	cin>>partNum;
	if (partNum[0] < 'A' || partNum[0] > 'C')
		valid = 0;
	if (partNum[1] < '1' || partNum[1] > '4')
		valid = 0;
	if (partNum[2] < '0' || partNum[2] > '9')
		valid = 0;
	if (partNum[3] < 'a' || partNum[3] > 'z')
		valid = 0;
	if (partNum[4] < '2' || partNum[4] > '6')
		valid = 0;
	if (partNum[5] < '0' || partNum[5] > '9')
		valid = 0;
	if (partNum[4] == '2' && partNum[5] < '2')
		valid = 0;
	if (partNum[4] == '6' && partNum[5] > '6')
		valid = 0;
	if (valid == 0)
		cout<<"You have entered an invalid part number."<<endl;
	else
		cout<<"You have entered a valid part number."<<endl;
	system("pause");
		return 0;
}


This line of code in particular I was having problems with. This line works.

1
2
if (partNum[0] < 'A' || partNum[0] > 'C')
		valid = 0;


While this line does not.

1
2
if {partNum[0] != 'A'||partNum[0] != 'B' ||partNum[0] != 'C'}
		valid = 0;


I was just wondering if != operator has a problem when comparing characters in ASCII.
Last edited on
No, it doesn't. You have a syntax error there though - and it's questionable if those lines actually do what you want them to do - for example:

 
if (partNum[0] < 'A' || partNum[0] > 'C')

This is always executed if the first character is not A, B or C. Dunno if that's what you want.


 
if {partNum[0] != 'A'||partNum[0] != 'B' ||partNum[0] != 'C'} //those should be () brackets, not {}! 

aside from that, this statement is ALWAYS true, no matter what partNum[0] actually is - because the statement is exactly then true when ONE OR MORE of the following is true:
partNum[0] is
1) not A
2) not B
3) not C

for example, if it was A, 1 would be false but 2 and 3 would be true. If it was B, 2 would be false but 1 and 3 would be true. If it was C, 3 would be false but 1 and 2 would be true. If it was any other character, 1,2 and 3 would be true. No matter what, you always get the same result - you might as well write if(true)
Last edited on
Yeah that line was me just typing in because I don't have it in my code anymore, I was just hurrying and forgot that I was supposed to use (). In the actual code I had them though. The program is supposed to have either A B or C as the first character. This was the only line in the code that was throwing a fit. After changing the != operator to instead test for being <A or >C it worked just fine.

But what you're saying is not the operator itself that was the problem. I was thinking that writing this line
(partNum[0] != 'A'||partNum[0] != 'B' ||partNum[0] != 'C')
would set false to 0 if it were anything other than A B or C.

with this line though, even when I entered A B or C as the first character it would set valid to 0, outputting my invalid statement.

Just bad use then?
Last edited on
Exactly. What you wanted was logical AND (&&) rather than logical OR (||).
Topic archived. No new replies allowed.