#include <iostream>
#include <fstream>
#include <windows.h>
#include <iomanip>
using namespace std;
void printMainPage(); //customer need to register 1st Page
void printUserPage(); //User(registered customer) page
void Login();
bool openFileIn(fstream &, string );
//fstream in_file("Register and Login Details.txt");
fstream file("Register and Login Details.txt", ios::out | ios::in);
int main()
{
printMainPage();
system("cls");
printUserPage();
return 0;
}
void printMainPage() //Main page
{
int choiceMainPage,
usercount=0; //userCount
static int userReg=0; //howmuch have register
string reg_pwd, //password
reg_name; //username
cout<<"********WELCOME TO MEK DINAH**********\n\n";
cout<<"\t1. Register \n";
cout<<"\t2. Login \n";
cout<<"\t3. Exit program.\n";
cout<<"\n\n\t\t Registered User : " << userReg;
cout<<endl;
cout<<"\n**************************************\n";
cout << "Your choice: ";
cin>>choiceMainPage;
while(choiceMainPage<1 || choiceMainPage>3)
{
cout << "Invalid, re-enter again: ";
cin >> choiceMainPage;
}
if(choiceMainPage==1) //Register
{
system("cls");
cout << "Please key-in your information" << endl;
cin.ignore();
cout<<"Username : ";
getline(cin,reg_name);
cout<<endl;
cout<<"Password : ";
getline(cin,reg_pwd);
cout<<endl;
cout << "Username: " << reg_name << "\npassword: " << reg_pwd << endl;
file << reg_name << "#" << reg_pwd << "#";
userReg++;
cout << "You will be brought back to our main page...\nPlease wait";
Sleep (3000);
system("cls");
printMainPage();
}
else if(choiceMainPage==2) //login
Login();
else //exit
exit(0);
file.close();
}
void Login()
{
char login_un[50],
login_pw[50],
username[10][50],
password[10][50]; //nanti tarik dpd fail
int choiceLogin,
conditionLogin;
cout<<"Main\n\n"
<<"1.Login\n"
<<"2.Main Page\n";
cin>> choiceLogin;
while(choiceLogin<1 || choiceLogin>2)
{
cout << "Invalid, re-enter again: ";
cin >> choiceLogin;
}
if (choiceLogin==1)
{
int userCount=0;
if(openFileIn(file, "Register and Login Details.txt"))
{
while(file)
{
file.getline(username[userCount], 50, '#'); //read existing username
file.getline(password[userCount], 50, '#'); //read existing passowrd
userCount++;
}
}
cout << "checking existing data" << endl;
Sleep(1000);
for(int x=0; x<userCount; x++)
cout << "username: " << username[x] << "\npassword: " << password[x] << endl;
do
{
cin.ignore();
cout<<"Username: ";
cin.getline(login_un, 50);
cout<<endl;
cout<<"Password: ";
cin.getline(login_pw, 50);
for(int x=0; x<userCount; x++)
{
if (strcmp(login_un, username[x])==0)
{
if(strcmp(login_pw, password[x])==0) //comparing both username n password
{
conditionLogin=0;
break;
}
}
cout << "x : " << x << " UserCount: " << userCount << endl;
}
cout << "Username and password doesn't match, Please reenter again:" << endl;
conditionLogin=1; //if 1 then loop again
cin.ignore();
}while(conditionLogin==1);
}
else
{
printMainPage();
}
return;
file.close();
}
void printUserPage() //User Page
{
cout<<"********WELCOME TO MEK DINAH**********\n\n";
cout<<"\t1. Displaying Menu \n";
cout<<"\t4. Order \n";
cout<<"\t5. Display Receipt\n";
cout<<"\t3. Exit to Main page.\n";
cout<<endl;
cout<<"\n**************************************\n";
}
bool openFileIn(fstream &in_file, string name)
{
in_file.open(name.c_str(), ios::in);
if(in_file.fail())
return false;
else
return true;
}
mr.muin13
0
Newbie Poster
Recommended Answers
Jump to PostAre you sure it is opening the file? Put a cout statement between lines 112 and 113 and see if it prints the stament. This will let you now if the file was opened succesfully. It should work if the file is being opened. What does the file look like?
Jump to PostYou could try using
ifstream
for file input, andofstream
for file output.
Also, I would suggest using a map for storing(in memory) your users and their respective passwords.
Another thing, try usingstring
s instead of char array. They are more reliable, and you can do much …
Jump to PostLucaci's suggestions are good. I have some additional comments.
If you want to understand why your original/existing code is failing:
Part of your problem is that you are using a single, global stream to deal with file I/O. Another, bigger part of the problem is that you are not closing …
All 11 Replies
mr.muin13
0
Newbie Poster
NathanOliver
429
Veteran Poster
Featured Poster
mr.muin13
0
Newbie Poster
NathanOliver
429
Veteran Poster
Featured Poster
mr.muin13
0
Newbie Poster
NathanOliver
429
Veteran Poster
Featured Poster
mr.muin13
0
Newbie Poster
Lucaci Andrew
140
Za s|n
JasonHippy
739
Practically a Master Poster
mr.muin13
0
Newbie Poster
mr.muin13
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.