View Full Version : simple password checker

September 2nd, 2006, 03:47 PM
i created a program that prompts the user to give a psswrd, then the program stores the password. the program then checks the password. but when it gets to the part where it checks the password, no matter what i do, it never sees that the two strings are the same...i was wondering if i someone could point me in the right direction?

anyways, here is the code:

//store/check password
#include <iostream>
#include <cstdlib>
using namespace std;

class pswrd {
char ps[80], usr[80];
int len;
void set_ps (char *p) {strcpy(ps, p);}
void check_ps () {
printf(""\nPassword: "");
if(usr!=ps) cout<<endl<<""Access Denied"";
else if(usr==ps) cout<<endl<<""Access Granted"";

int main ()
pswrd ob1;
char p[80];
printf(""Set Password: "");


September 2nd, 2006, 04:00 PM
Somebody might be able to help you with your C++ code but please make an effort to get it in the right part of the forum. This is the second post I've moved from you in the last two minutes.

Moving to general off topic.

Yes, it's a programming question but it doesn't appear to be vintage computer related.

September 3rd, 2006, 04:56 AM
It was years since I touched C++, but can you compare the content of two arrays with the = operator? In traditional C, you would use strcmp to compare the two strings (implemented as array of char). I'm afraid you need to do the same in C++, or in this case you might do a comparison of the memory addresses where ps[80] and usr[80] are stored.

September 3rd, 2006, 11:18 AM
I had posted exactly the same thing but I deleted it because I had never looked into C++. Unless C++ has some powerful operators, like the man said, you are comparing the address of one array with the address of another one. Surprise, not equal all the time.

September 3rd, 2006, 02:21 PM
You can overload operators in C++, but I'm unsure if the = operator is overloaded for arrays. IIRC there is a specific String (capital S) class/variable type you can use, and it would allow the simple type of comparison. The poster - like many of us - seems to program in a mix of C and C++ which tends to confuse you at some point.