Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   simple password checker (http://www.programmingforums.org/showthread.php?t=11021)

RemoteC2 Aug 9th, 2006 11:43 PM

simple password checker
 
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 {
public:
      char ps[80], usr[80];
      int len;
      void set_ps (char *p) {strcpy(ps, p);}
      void check_ps () {
            printf("\nPassword: ");
            cin>>usr;
            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: ");
    cin>>p;
    ob1.set_ps(p);
    ob1.check_ps();
    system("PAUSE");
}


Prm753 Aug 10th, 2006 12:07 AM

Try using the strcmp() function. :)

andro Aug 10th, 2006 12:21 AM

Or stop using C headers and functions, seeing as this is posted in the C++ forum, and use std::string.

Then you can use != and == on them.

ionexchange Aug 10th, 2006 12:24 AM

You might also want to initialize the strings ps and usr.

andro Aug 10th, 2006 4:43 AM

Quote:

Originally Posted by ionexchange
You might also want to initialize the strings ps and usr.

BS...

DaWei Aug 10th, 2006 8:02 AM

@ionexchange: I checked the rules and the one forbidding posting voodoo solutions seems to have disappeared. Please don't do it, anyway.

RemoteC2 Aug 10th, 2006 10:36 AM

the reason that i didnt initialize ps and usr, is b/c the program makes it so that the user inputs the password. i made this program just to see if i could..anways, thx everyone for the help. the program works now, and the final source is this:

:

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

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

int main ()
{
    pswrd ob1;
    char p[80];
    printf("Set Password: ");
    cin>>p;
    ob1.set_ps(p);
    ob1.check_ps();
    system("PAUSE");
}


DaWei Aug 10th, 2006 11:14 AM

Quote:

else if cout<<endl<<"Access Granted";
You should remove that token. It's not functional, therefore it's confusing, therefore it's worthless.

Prm753 Aug 10th, 2006 11:20 AM

Quote:

Originally Posted by RemoteC2
the program works now, and the final source is this:

Quote:

Originally Posted by Dev-C++ compiler error
expected `(' before "cout"

This was the error:
:

  1. else if cout<<endl<<"Access Granted";

Easy to fix, though.

EDIT: Too slow, DaWei beat me to it...

andro Aug 10th, 2006 4:09 PM

You're still using C functions and C strings in a C++ program.

And what happens when I come along and enter my 81 character password? Looks like bad news.


All times are GMT -5. The time now is 12:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC