Login System Fail to Execute.

hi there, i have created a unix-like login system by using c++ and it works fine for the first execution but when i close and execute it again. it gives me error where i cant login with the stored username and password. how can i fix this?
here is my code:

#include <iostream> // cout, cin
#include <fstream> // read and write to files
#include <conio.h> // getchar()
#include <io.h> // check file existence
#include <string> // use strings ?

using namespace std; // so we can use cout instead of std::cout
void checkpassfile(); // function to check password file .....
void menu(); // displays menu ie: lock system, change pass etc...
char user[30],usr[30];
char password[30], pass[30]; //... two variables, 1 holding user pass & other holding real pass
int attempts=0; // holds incorrect attempt numbers


int main()

{
system("CLS");
cout << "\t\t ::: Mynix Login System ..... :::" << endl;
cout << "\n\t\t\t(( **************** ))" << endl << endl;
cout << "\n===============================================================================" << endl;

checkpassfile();

return 0;
}

// Checks for password file .......

void checkpassfile()
{
ifstream read;
ofstream write;
ofstream logfile;

if(_access("pass.txt", 00)==0)
{
read.open("pass.txt");

if (read.fail())
{
cout << "Error Reading File ..... Restarting!\n";
}
read.close();
while(!read.eof())
{
read >>usr>> pass;
}


cout << "*******************************************************************************" << endl;
cout << "Username: ";
cin>>usr;
cout<<"Passwd: ";
cin >> password;

if ((strcmp(usr, user)==0)&&(strcmp(password,pass)==1))
{
cout << "\n\nUsername And Password Accepted!" << endl;
cout << "\t\t\nLogin Acknowledged, Welcome" << endl;
getchar();
menu();
}
else
{
logfile.open("log.txt", ios::app);
logfile << "Invalid Password: " << password << " at "<< endl;
logfile.close();

attempts = attempts + 1;
if (attempts==3)
{
system("CLS");
cout << "\a\a\a\a\a\nYou have entered 3 incorrect Password attempts .... Quiting!" << endl;
exit(0);
}
else
{
cout << "\a\a\nPassword Not Accepted!" << endl;
cout << "\t\t\t\nYour attempt has been Logged!" << endl;
checkpassfile();
}
}
}
else
{
cout << "\a\tA Password file was not Detected .... Creating a new One..... !" << endl;
cout<<"\nEnter User Name: ";
user[0]=0;
cin>>user;
cout << "\nEnter new password: ";
password[1]=0;
cin >> password;
write.open("pass.txt");
write<<user;
write<<password;
write.close();

cout << "\nYour Password has been Successfully Created!" << endl;
getchar();
system("CLS");
main();
}
logfile.close();
}

// Displays Menu, Options .........

void menu()
{
system("CLS");
int choice;

cout << "\n=================================================" << endl;
cout << "\n\nMyUnix Shells Menu:" << endl;
cout << "\t1. - Exit Programme" << endl;
cout << "\t2. - Logout System" << endl;
cout << "\t3. - Clear Log File" << endl;
cout << "=================================================" << endl;
cout << "\n\nChoice: ";
cin >> choice;

switch (choice)
{
case 1: exit(0); break;
case 2: main(); break;
case 3:
{
remove("log.txt");
cout << "\n\nLog File Cleared!\n";
getchar();
menu();
break;
}
default: cout << "\nUnknown Option... Returning to Menu!" << endl; getchar(); menu(); break;
}

}
Last edited on
Please use code tags so your code is readable
A unix like login and your using conio.h !!! wtf I almost dropped dead....
DON'T USE IT! it's so so so bad, outdated and not even supported by unix/mac systems let alone most windows.(The current windows API is much better, if you want to be windows only)
Don't use system commands. there's many many posts why, check out the search.
http://www.cplusplus.com/forum/articles/11153/

1
2
3
4
5
read.close();
while(!read.eof())
{
   read >>usr>> pass;
}


probably a bad idea ^^
Last edited on
Topic archived. No new replies allowed.