1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
void AccountManager::changePassword(AccountManager & account) {
string username, password, newPass, passwordConf, tempUser, tempPass, hold;
fstream openFile("UserPass.txt", ios_base::out | ios_base::in | ios_base::app);
// / Check if username exsists.
do {
cout << "Enter your username: " << endl;
getline(cin, username);
cout << "Enter you current password: " << endl;
getline(cin, password);
if (account.UserPass[username] != password) {
cout << "Username and password do not match. " << endl;
}
} while (account.UserPass[username] != password);
do {
cout << "Enter new password: " << endl;
getline(cin, newPass);
cout << "Retype password: " << endl;
getline(cin, passwordConf);
if (newPass != passwordConf) {
cout << "Password does not match confirmation. " << endl;
}
} while (newPass != passwordConf);
// /find / replace password with newPass in file
while (!openFile.eof()) {
getline(openFile, tempUser, ';');
getline(openFile, tempPass);
if ((tempUser == username) && (tempPass == password)) {
fstream openFile("UserPass.txt", ios_base::out | ios_base::in);
int pos = tempUser.length() + tempPass.length()+2; // pos for beginning of the line
openFile.seekg(-pos, ios::cur); // Go back from current position by x amount
getline(openFile, hold);
char * cPass = new char[newPass.length()];
strcpy(cPass, newPass.c_str());
int index = hold.find(';')+1; // changes pass in file at index ;+1
openFile.seekp(index);
openFile.write(cPass, tempPass.length());
cout << "Password has been changed. " << endl;
switchLog(account); // Login on successful password change.
//delete[] cPass breaks program
break;
}
}
account.UserPass[username] = newPass;
}
|