Password check fail... wheres the problem?

i need this function in a program i'm writing
it checks the password you enter and provides access to another part(not shown here).
when i enter the set password(aaaaaa) while running the program, it goes to the else case and says incorrect password and returns 0. I tried running the program by using gets(pass); and that worked but that's not a proper way to enter a password...
help would be sincerely appreciated.

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
26
27
28
int checkpass()
{
	clrscr();
	char pass[6];
	cout<<"\n\nType 6-character Access Key: ";
	for(int i=0;i<=5;i++)
	{
		pass[i]=getch();
		cout<<"*";
	}
	clrscr();
	cout<<"\n\nVerifying...";
	delay(2000);
	clrscr();

	if(strcmpi("aaaaaa",pass)==0)
	{
		cout<<"\n\nConnecting To Database Server...";
		delay(2000);
		return(1);

	}
	else
	{
		cout<<"\n\nDetected Incorrect Passkey...\n\a";
		return(0);
	}
}




Last edited on
Your password does not terminate with null char, therefore it is not a c-string.
First solution: change strcmp to memcmp("aaaaaa", pass, 6)
Second solution: char pass[7]; pass[6] = 0;
Also, your code doesn't accept backspaces. I'm writing article about this so it so this may be interesting for you: http://cplusplus.com/articles/preview/40/
Last edited on
Thanks a lot!!! It works... i never thought of that.
Topic archived. No new replies allowed.