Compare

I have looked at the compare tutorial here on C++, but I must still be missing something.

Originally this code worked. It would compare the input to information within a file. The file location is correctly designated, as I have other functions accessing files within the same location.

What am I missing?

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
void adminAuthVer() //this code (when dual created) will test authoirization of Admin and User
{
	string input;
	bool found = false;
	
	{
		cout << "Enter Passcode: ";
		cin >> input;
	}

	string stringfromfileA;
	ifstream openFile(PATH + "Admin Info.txt");
	getline(openFile, stringfromfileA);
	if (input.compare(stringfromfileA))  //passcode loop
	{
		found = true;
		cout << "Success! \n" << endl;
		adminControl();
	}

	if (input != stringfromfileA)
	{
		cout << "Access Denied!!\n" << endl;
	}
}
Line 14: Your if statement is faulty. Read the reference page for compare, specifically the section on return value.
http://www.cplusplus.com/reference/string/string/compare/
compare returns 0 if the strings are equal. Your if statement is checking for a non-zero return value.

Lines 4,16: What's the point of found? You never use it.

Line 13: You don't check that the getline() call succeeded.

Line 18: It's poor style to pass control to something that performs admin functions here. adminAuthVer() should do one thing and one thing only and that is to check the password. adminAuthVer() should be a bool function and return whether to allow access or not. adminControl() should be called where adminAuthVer() is called if adminAuthVer() returns true.

Line 21: A second if statement is not required. Use an else.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool adminAuthVer()
{	string input;
	string stringfromfileA;
	
	cout << "Enter Passcode: ";
	cin >> input;
	ifstream openFile("Admin Info.txt");
	if (! getline(openFile, stringfromfileA))
	{	cout << "Unable to open Admin Info.txt" << endl;
		return false;
	}
	if (input == stringfromfileA)  
	{	cout << "Success! \n" << endl;
		return true; 
	}
	else
	{	cout << "Access Denied!!\n" << endl;
		return false;
	}
}
AbstractionAnon
Line 14: if (input.compare(stringfromfileA))
originally this was supposed to compare my input, to the string within the designated file.
It worked once and then out of nowhere decided not to work.

Line(s) 4, 16:
bool found = false; found = true;
At line 4 the false bool was already in place until the compare in line 14 would verify input and string were ==, switching the bool to true and allowing access.

When adminAuthVer is true, it would open adminControl.
Again, std::string::compare() tells you the relative ordering of two strings. It returns zero if and only if the strings are the same.

The integer value 0 converts to false. All other integers convert to true.

To use compare correctly, compare the result to zero:
if (input.compare(stringfromfileA) == 0) // strings are equivalent

But in general, to test if two things are equivalent, you'd use the equality operator ==, like @AbstractionAnon.
Last edited on
You're missing my point about lines 4 and 16. You declare and set found, but never reference it.
What is the point of a variable you never reference? You might as well just delete it.
Registered users can post here. Sign in or register to post.