Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 107,669 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,041 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Help with this Username & Password function please.

 
Reply to this topicStart new topic

Help with this Username & Password function please., Code taken from an article on </dream.in.code>

cJr.
post 6 Aug, 2008 - 12:46 PM
Post #1


New D.I.C Head

*
Joined: 10 May, 2008
Posts: 21


My Contributions


I am implementing a username & password function into my program & I found this really good article on </dream.in.code>. The only problem is, it doesn't seem to work when I have modifed it to work with my program.

This is the article I have taken the code from: http://www.dreamincode.net/code/snippet536.htm

This is the code I have in my program, modified from this article (along with my main() function, as some of the code in that has code for the password function):

CODE
void Auth(int& birthDate, int& birthMonth, int& birthYear, int& currentMonth, int& currentYear, int& userAge);
void Members(int& birthDate, int& birthMonth, int& birthYear, int& currentMonth, int& currentYear, int& userAge);
void Userchange();
void Passchange();

string inuser;
string inpass;
string user;
string pass;
int num = 0;
string com;


int main ()
{  
    
    //function declarations
    void displayHeaderFooter( int& currentMonth, int& currentYear);
    void ageVerification( int& birthDate, int& birthMonth, int& birthYear, int& currentMonth, int& currentYear, int& userAge);
    void displayMenu( int& keyPressed, int& currentMonth, int& currentYear);
    void wordCounter( int& keyPressed, int& currentMonth, int& currentYear);
    int  getKeyPress();
    void endProgram(int& keyPressed, int& currentMonth, int& currentYear);


    //local variable declarations
    int currentMonth, currentYear;              
    int birthDate, birthMonth, birthYear;                 //the user's date of birth
    int userAge;                                        //the user's current age
    int keyPressed;                                    //for the 'getKeyPress' function  
    


    //----------------
    // MAIN PROCEDURE:
    //----------------
  
    
    //PERFORMING PASSWORD CHECK BEFORE AGE APPROVAL

      while(num==0)
      {
      CLEAR_SCREEN();
      cout<<"Command: ";
      cin>>com;
      if(com=="login")
      {
      Auth(birthDate, birthMonth, birthYear, currentMonth, currentYear, userAge);
      }
      else if(com=="change-pass")
      {
           Passchange();
           }
      else if(com=="change-user")
      {
           Userchange();
           }
      else if(com=="credits")
      {
           cout<<"written by Jason Parker"; // cuz it was
           _getch();    
      }
      else if(com=="exit")
      {
           cout<<"written by Cipherence SoftWurx";
           _getch();
           break;
           }
      else if(com==com)
      {
           cout<<"Unknown command\n";
           }
      }

    

    //DISPLAYING THE MENU AFTER AGE & PASSWORD APPROVAL
    keyPressed = getKeyPress();
    if (keyPressed == 'M')
        {
            CLEAR_SCREEN();
            displayHeaderFooter(currentMonth, currentYear);
            displayMenu(keyPressed, currentMonth, currentYear);
        }


cin.get();
return 0;    

}

void Auth(int& birthDate, int& birthMonth, int& birthYear, int& currentMonth, int& currentYear, int& userAge)
{
   void Members(int& birthDate, int& birthMonth, int& birthYear, int& currentMonth, int& currentYear, int& userAge);
    
     ifstream Passfile("password.txt", ios::in);
     Passfile >> inpass;
     ifstream Userfile("username.txt", ios::in);
     Userfile >> inuser;
     CLEAR_SCREEN();
     cout << "USERNAME: ";
     cin >> user;
     cout << "PASSWORD: ";
     cin >> pass;
     Userfile.close();
     Passfile.close();

     if (user == inuser && pass == inpass)
     {
     cout<<"\nPress the 'Enter' key to display the Main Menu";
     _getch();
     Members(birthDate, birthMonth, birthYear, currentMonth, currentYear, userAge);
     }
     else
     {
         cout << "nope";
         _getch();
         main();
         }

}
void Userchange()
{
     string tempass;
     CLEAR_SCREEN();
     cout<<"Please enter your current password";
     cin>>tempass;
     if(tempass==inpass)
     {
     cout<<"utility to change username stored in file\n";
     cout<<"CHANGE USER TO: ";
     cin>>user;
     ofstream Userfile("username.txt", ios::out);
     Userfile<<user;
     Userfile.close();
     cout<<"You have successfully changed your username";
     _getch();
     main();
}
else
{
    cout<<"The password you entered is invalid. Please enter the correct password";
    _getch();
    main();
}
}
void Passchange()
{
     string tempass;
     CLEAR_SCREEN();
     cout<<"Please enter your current password";
     cin>>tempass;
     if(tempass==inpass)
     {
     cout<<"utility to change password stored in file\n";
     cout<<"CHANGE PASS TO: ";
     cin>>user;
     ofstream Passfile("password.txt", ios::out);
     Passfile<<user;
     Passfile.close();
     cout<<"You have successfully changed your password";
     _getch();
     main();
}
else
{
    cout<<"The password you entered is invalid. Please enter the correct password";
    _getch();
    main();
}
}

void Members(int& birthDate, int& birthMonth, int& birthYear, int& currentMonth, int& currentYear, int& userAge)
{
    void displayHeaderFooter( int& currentMonth, int& currentYear);
    void ageVerification( int& birthDate, int& birthMonth, int& birthYear, int& currentMonth, int& currentYear, int& userAge);
    
    displayHeaderFooter( currentMonth, currentYear);
    ageVerification( birthDate, birthMonth, birthYear, currentMonth, currentYear, userAge);
}



Basically, the problems I have is that when I enter the username & the password which are stored in the text files, it still outputs "nope", which is supposed to happen when the user has entered the wrong password &/or username (obviously in my final code, it won't just say unhelpfully say "nope" lol).

Also, when trying to change the username or password, after asking for the current password, it says the password I have entered is invalid, when it is the same as what is in the text file.

The commands in the code such as "login" and "change-user" seem to work, it just doesn't seem to recognise the password (& maybe the username?) & does not display the correct thing (the 'displayHeaderFooter' & 'displayMenu' functions).

I have put the two text files in the "Debug" folder of my project (that's right isn't it?) and I am using Visual Studio 2005.


Any help really would be much appreciated.
Thank you in advance.
cJr.

This post has been edited by cJr.: 6 Aug, 2008 - 12:51 PM
User is offlineProfile CardPM

Go to the top of the page


jwwicks
post 6 Aug, 2008 - 03:33 PM
Post #2


D.I.C Head

Group Icon
Joined: 31 Jul, 2008
Posts: 51



Thanked 5 times

Dream Kudos: 200
My Contributions


Hello cJr,

QUOTE(cJr. @ 6 Aug, 2008 - 12:46 PM) *

I am implementing a username & password function into my program & I found this really good article on </dream.in.code>. The only problem is, it doesn't seem to work when I have modifed it to work with my program.


I have put the two text files in the "Debug" folder of my project (that's right isn't it?) and I am using Visual Studio 2005.

Any help really would be much appreciated.
Thank you in advance.
cJr.


Leave the txt files in your project folder not the Debug folder. VS runs the app from that folder by executing the command Debug/my.exe

See if that helps...

JW
User is offlineProfile CardPM

Go to the top of the page

cJr.
post 7 Aug, 2008 - 07:13 AM
Post #3


New D.I.C Head

*
Joined: 10 May, 2008
Posts: 21


My Contributions


Thanks a lot jwwicks! smile.gif It works perfectly now I changed the folder of the text files.

Now I'm just trying to figure out a way I could have more than one user on the system?

Can anybody help me with that please? I can get it to change the password & username stored in the files (so I could have an option to create a new user) but that would still only allow the one user because they is obviously only the one text file for password & the one text file for username.

Has anybody got any ideas please?
User is offlineProfile CardPM

Go to the top of the page

ayya14
post 11 Aug, 2008 - 06:00 AM
Post #4


New D.I.C Head

*
Joined: 12 Jul, 2008
Posts: 1

how do you do it with a single txt file to hold username and password?
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/29/08 11:00PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month