Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- using namespace std;
- void rgs();
- void Menu();
- void checkInfo();
- void Login();
- int ans1;
- string charname, pw, user, pass;
- bool access;
- int main()
- {
- Menu();
- system("CLS");
- if (ans1==1)
- {
- Login();
- }
- else if (ans1==2)
- {
- rgs();
- }
- else
- {
- cout<<"You have entered an invalid option"<<endl;
- }
- system ("pause");
- return 0;
- }
- void Login()
- {
- cout<<"Please enter your charater name: ";
- cin>>user;
- cout<<"Please enter your password: ";
- cin>>pass;
- ifstream inFile;
- string username, password;
- inFile.open("users.txt");
- if (inFile.is_open())
- {
- while (!inFile.eof())
- {
- inFile >>username>>password;
- if (user==username && pass==password)
- {
- access=true;
- }
- }
- if (access)
- {
- cout<<"You have login successfully!"<<endl;
- }
- else
- {
- cout<<"You have enter the wrong username/password!"<<endl;
- }
- }
- else
- {
- cout<<"Error!"<<endl;
- }
- inFile.close();
- }
- void checkInfo()
- {
- }
- void Menu()
- {
- cout<<"Welcome to dungeon hunter!"<<endl;
- cout<<"Choose a choice below."<<endl;
- cout<<"1. Login"<<endl;
- cout<<"2. Register a new account"<<endl;
- cout<<"Please enter your option: ";
- cin>>ans1;
- }
- void rgs()
- {
- ifstream inFile;
- ofstream newuser;
- string username, password, passwordconfirm;
- inFile.open("users.txt", ios::app);
- newuser.open("users.txt", ios::app);
- bool uservalid=false;
- while (!uservalid)
- {
- cout << "Username: ";
- cin >> username;
- cout << "Password: ";
- cin >> password;
- cout << "Confirm password: ";
- cin >> passwordconfirm;
- int m=0;
- int k=0;
- while (inFile>>user>>pass)
- {
- m++;
- if (username!=user)
- k++;
- }
- if (m==k && password==passwordconfirm)
- uservalid=true;
- else if (m!=k)
- cout << "There is already a user with this username." << endl;
- else
- cout << "The passwords given do not match." << endl;
- }
- newuser << username << " " << password << endl;;
- inFile.close();
- newuser.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement