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.htmThis 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