Trouble with the following function?

Aug 23, 2013 at 2:22am
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;

}
Aug 23, 2013 at 2:49am
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 Aug 23, 2013 at 2:50am
Aug 23, 2013 at 4:39am
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 Aug 23, 2013 at 5:03am
Aug 23, 2013 at 4:48am
What is the exact output and the desired output?
Aug 23, 2013 at 5:11am
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.
Aug 23, 2013 at 5:17am
Oh! silly me.

Replace all the ||s with &&s. Reason it out in your head and you will know why. :)
Aug 23, 2013 at 5:17am
Have you tried the code I suggested? This should have solved your problem
Aug 23, 2013 at 5:23am
is "and" meant to be psuedocode?
Just wondering... ;p
Aug 23, 2013 at 5:55am
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
Aug 23, 2013 at 6:11am
Aug 23, 2013 at 6:25am
@keskiverto: I stand corrected. (Though you didn't include ciso646)

The AND operator works. You used the OR operator
Aug 23, 2013 at 6:54am
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 Aug 23, 2013 at 6:54am
Aug 23, 2013 at 5:52pm
Thanks for all your help @Smac89, I will keep that in mind next time.
Topic archived. No new replies allowed.