Password System Error

I am trying to write a program which includes a three stage password.
The user is prompted with a question that has a single word answer. They must input the correct string to proceed to the second question, and again to reach the third.
In order to verify that the user has entered the correct code, I use the string compare function. However, the second or third stages frequently fail to correctly compare the input password to the correct existing password. The code is allowing the user to progress to the third stage regardless of what they enter as string 2.
One complexity added is that there are multiple accepted passwords to allow for flexibility with the input, such as the capitalization of the first character in the string.

Example code is provided below:
string name, trueName1, trueName2;
string trueName1= "John";
string trueName2= "john";

cout<< "Stage 1:" <<endl;
cout<< "Enter first name: ";
cin>> name;
if (( == 1) && ( ==1) )
	cout<<"Error - ID mismatch" <<endl;
	return 0;

This will work once, sometimes even twice in a row, but very frequently the second or third stage will fail to present any error, and allows the user to pass by entering any string.

Any suggestion as to what my issue might be would be greatly appreciated.

Last edited on
If I'm not mistaken, you're telling the computer to kick out the user on the instance that BOTH names match...which is not, of course, possible, unless there's something janky going on.
The compare function returns a 1 only if there is a mismatch between the first and second string, in this case name and trueName1/2. In this code, you are booted out if the password you enter generates a mismatch with both accepted passcodes. if it matches either of the provided (john, John) it skips the contents of the If and proceeds on to stage two/three etc.
Well, then you're probably having difficulty elsewhere in the code, like in the name class (which you, for some reason, named the same as your input string) or in the name::compare(string); function.
there is no class called 'name' in the code.
it is declared as a string in the beginning of the code i supplied.
In my code, the supplied section is immediately repeated using nothing different except for the strings. And the code will sometimes allow a nonmatching password from the user to skip the THEN portion of the IF statement. There is no external code acting on anything related to these variables or functions, so I am fairly confident that the error does not lie elsewhere.
Of course, there is a chance that i am wrong, given that I am unable to find the issue.
If anyone happens to have a suggestion for a more reliable method of string comparison, that would be equally appreciated.
If you really want to learn about string comparison, you could always
$man -m3 strncmp

I don't think that's going to work for you, though. I doubt you'd even be able to do it on your computer, although you could look it up online.

So if you don't have any external code effecting it, explain this:
Looks like a function call to me.

I think you're possibly having string or buffer overrun issues, since you said that you reuse this code another couple of times without any changes to anything except for the string contents.
alright, there is some confusion.
" the name class (which you, for some reason, named the same as your input string)"
the compare function is a function from the string class library. so yes, there is a class object called 'name', but it IS a string.

I have also tried flushing the cin buffer between every instance, but the issue persisted. I would expect buffer issues to include leftover characters in the cin buffer that would be entered into the followup cin calls, causing a password error that would automatically kick you out of the program. however, my issue is that I can't get the program to boot you out reliably.
I don't know if this persists in C++, but in C there is a huge difference between:
char * strName1 = "Joe";
char * strName2 = {'Joe'};

char pointers are how strings are implemented in C, so it's pretty equivalent. Given the massive difference between the two, you could also be having difficulty with your terminator.
Ah. I have found my issue. I was incorrectly remembering the functionality of the compare function. I mistakenly believed that it returns 1 for a mismatch and 0 for a match. This is not the case. it does return zero for a match, but the mismatch return value can vary, meaning that the code can be fixed by simply changing one portion of the IF condition. Instead of comparing every returned value to 1 ( == 1), i simply had to compare to zero, as this is the only reliably fast check for matching. ( != 0)

Thanks for your help ciphermagi, I always find it easier to figure out my error when i have a wall to bounce my thoughts off of.
Don't forget to click 'solved'
Topic archived. No new replies allowed.