Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 9th, 2006, 10:43 PM   #1
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
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");
}
RemoteC2 is offline   Reply With Quote
Old Aug 9th, 2006, 11:07 PM   #2
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 3 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Try using the strcmp() function.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Aug 9th, 2006, 11:21 PM   #3
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 290
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
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.
andro is offline   Reply With Quote
Old Aug 9th, 2006, 11:24 PM   #4
ionexchange
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 ionexchange is on a distinguished road
You might also want to initialize the strings ps and usr.
ionexchange is offline   Reply With Quote
Old Aug 10th, 2006, 3:43 AM   #5
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 290
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Quote:
Originally Posted by ionexchange
You might also want to initialize the strings ps and usr.
BS...
andro is offline   Reply With Quote
Old Aug 10th, 2006, 7:02 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
@ionexchange: I checked the rules and the one forbidding posting voodoo solutions seems to have disappeared. Please don't do it, anyway.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 10th, 2006, 9:36 AM   #7
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
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");
}
RemoteC2 is offline   Reply With Quote
Old Aug 10th, 2006, 10:14 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
else if cout<<endl<<"Access Granted";
You should remove that token. It's not functional, therefore it's confusing, therefore it's worthless.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 10th, 2006, 10:20 AM   #9
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 3 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
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:
cpp Syntax (Toggle Plain Text)
  1. else if cout<<endl<<"Access Granted";
Easy to fix, though.

EDIT: Too slow, DaWei beat me to it...
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Aug 10th, 2006, 3:09 PM   #10
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 290
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
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.
andro is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 8:55 AM
[Python] Password Generator bulio Show Off Your Open Source Projects 2 Feb 28th, 2006 3:01 AM
password box ragenuub Visual Basic 5 Nov 15th, 2005 3:46 PM
Just a small password generator Jessehk Show Off Your Open Source Projects 3 Sep 16th, 2005 8:41 AM
Simple Function Questions meverha1 C++ 16 Sep 12th, 2005 1:25 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:05 AM.

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