Trouble with the following function?

The following function is suppose to check if the the users input is valid. However, I am having trouble with the if statements as they keep executing even though the conditions are false.

If you need clarification let me know. Here is the source code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void RestockOption()
{
	int count = 0;
	Maintenance Staff;
	cout << "Warning: This section is case sensitive and failure to comply will \nresult in a system lock-down!" << endl;
	cout << "Name: ";
	getline(cin, Staff.name); 
	cout << "Maintenance ID: ";                                                                         
	cin >> Staff.ID;
	cin.ignore();
	cout << "Password: ";
	getline(cin, Staff.password);
	

	if(Staff.name != "Phillip" || Staff.name != "Mac")
		cout << "Name Error!" << endl;
	else if(Staff.ID != 303337 || Staff.ID != 303338)
		cout << "ID Error!" << endl;
	else if(Staff.name != "Treasure" || Staff.name != "Jackpot")
		cout << "Password Error" << endl;

}
1
2
3
4
5
6
if(Staff.name != "Phillip" and Staff.name != "Mac")
	cout << "Name Error!" << endl;
else if(Staff.ID != 303337 and Staff.ID != 303338)
	cout << "ID Error!" << endl;
else if(Staff.name != "Treasure" and Staff.name != "Jackpot")
	cout << "Password Error" << endl;


The last condition does not make sense to me
Last edited on
Ooops that was a typo...Here is the real corrections:

1
2
3
4
5
6
if(Staff.name != "Phillip" || Staff.name != "Mac Malloni")
		cout << "Name Error!" << endl;
	else if(Staff.ID != 303337 || Staff.ID != 303338)
		cout << "ID Error!" << endl;
	else if(Staff.password != "Treasure" || Staff.password != "Jackpot")
		cout << "Password Error" << endl;


Its still not outputting the desired effect.
Last edited on
What is the exact output and the desired output?
If the user doesn't enter the correct information name, ID, or password then an error message is suppose to appear. Such as "Name Error" "ID Error" "Password Error" .

The problem is, when I run the program, the error message appears despite the fact that I entered the correct information.
Oh! silly me.

Replace all the ||s with &&s. Reason it out in your head and you will know why. :)
Have you tried the code I suggested? This should have solved your problem
is "and" meant to be psuedocode?
Just wondering... ;p
Thanks for the help @Smac89. Sorry, I didn't notice your sample code was different from mine.

I realize why the add operator works. But why didn't my code work? Sorry if this is a silly question
@keskiverto: I stand corrected. (Though you didn't include ciso646)

The AND operator works. You used the OR operator
Whenever you find yourself having to deal with double negatives, always use and
When you find yourself dealing with double positives always use or

1
2
3
if ( a != b and a != c ) assert ( a == a );

if ( a == b or a == c ) assert ( a != a );
Last edited on
Thanks for all your help @Smac89, I will keep that in mind next time.
Topic archived. No new replies allowed.