Password hiding

Sep 26, 2011 at 12:19pm
hi i hav a code snippet for hiding the password as
#include<iostream>
#include<conio.h>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	int j;
	char c[10];

	cout << "password: ";

	for(j=0;j<8; j++ )
	{
		c[j] = getch();
		putch('*');
	}

	if(c== "password") 
		cout << "\nPassword accepted.\n";
	else
		cout << "\nAccess denied.\n";

	return 0;
}

If i enter "password"it shoult print password accepted but its not giving like that
why so??
can anybody tell me??
Sep 26, 2011 at 12:59pm
You think the expression (c == "password") means "Is the contents of the char array called c equal the contents of the constant char array "password"?" Well, that is not true. That expression means "Is the address pointed to by the variable c pointing to the same location of the constant string "password"? And the answer will never be No, they don't point to the same location.

To compare strings, use strcmp() or stricmp() or some other variant of those.
Sep 26, 2011 at 3:16pm
I am not sure, so correct me if this is wrong, but will
(*c == "password") give the correct results?
Sep 26, 2011 at 3:51pm
No, still incorrect. That shouldn't even compile due to type mismatch. Use strcmp() or stricmp().
Topic archived. No new replies allowed.