-
passgate program
I just began learning C++ about a week ago and have ran into a very strange problem. I was writing a database program and was adding password protection to it...only problem was, it didn't work. lol. So I made a VERY simple passgate to see what my problem was. About 2 hours later, I'm still working on it and making no real progress. Its kinda embarrasing to ask because its such a simple program, but I just am not seeing the solution.
Code:
#include <iostream.h>
int passcheck(char password[100]);
int passcheck(char password[100])
{
char correct[100] = "swordfish";
if (password == correct)
return 1;
else
cout << "\nWhat you typed: " << password;
cout << "\nWhat you should have typed: " << correct;
return 0;
}
int main()
{
char password[100];
cout << "\nPassword: ";
cin >> password;
passcheck(password);
cout << "\nExiting...";
return 0;
}
OUTPUT:
Password:
What you typed: swordfish
What you should have typed: swordfish
Exiting...
Maybe its just me, but I find it odd that it says I've typed in the right password, but that its somehow wrong. Any help would be appreciated.
-
You can't compare to char arrays like this.
Take a look at C++ style strings. It will be much easier.
http://www.cprogramming.com/tutorial/string.html
Also, put the indented statements which are part of the conditional in "{}" brackets
Code:
if (something)
{
do something
}
else
{
do something else
}
C++ is not indentation sensitive, so in your above code, the second statement after the else would have gotten executed no matter what.
-
You should use c++ strings really, but your prog should work if you change the line
Code:
if( password == correct )
to
Code:
if( !strcmp( password, correct ) )