#include<iostream>
#include<string>
#include<fstream>
#include<list>
#include<algorithm>
using namespacestd;
// filenames
conststring fnindex = "index.dat";
conststring fnusers = "users.dat";
// list of usernames
list<string>users;
boolAddUser (string username,string password) {
ofstream findex(fnindex,ios::app);
ofstream fusers(fnusers,ios::app);
string output;
bool res = true;
// primary index file is updated
if (findex.is_open()) {
output =username + "\n";
findex <<output;
findex.close();
} else {
res = false;
}
// new record is written"users.dat"
if (fusers.is_open()) {
output =username + "|" + "admin" + "|" +password + "\n";
fusers <<output;
fusers.close();
} else {
res = false;
}
returnres;
}
boolLoadUsernames() {
ifstream findex(fnindex);
string username;
// users are added to usernamelist
if (findex.is_open()) {
while (getline(findex,username)) {
users.push_back(username);
}
findex.close();
return true;
} else {
return false;
}
}
intmain() {
list<string>::iteratorpos;
string username;
string password;
bool newUser = true;
LoadUsernames();
while (newUser) {
cout << "Enterusername:" << endl;
cin >>username;
cout << "Enterpassword:" << endl;
cin >>password;
//username is checked
pos =find(users.begin(),users.end(),username);
if (pos== users.end()) {
AddUser(username,password);
newUser = false;
} else {
cout << "Username already exist" << endl;
}
}
return 0;
}
Comments
Leave a comment