codeview
在源代码的基础上进行了修改:
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
template <typename T>
T get_input(const std::string &strQuery);
std::string get_username();
std::string get_password();
void save_user(const std::string &username, const std::string &password);
void login();
void register_user();
void main_menu();
int main()
{
main_menu();
}
template <typename T>
T get_input(const std::string &strQuery)
{
std::cout << strQuery << "\n> ";
T out = T();
while (!(std::cin >> out)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits <std::streamsize>::max(), '\n');
std::cout << "Error!" "\n";
std::cout << strQuery << "\n> ";
}
return out;
}
std::string get_password()
{
std::string password1 = get_input <std::string>("Please enter your password.");
std::string password2 = get_input <std::string>("Please re-enter your password.");
while (password1 != password2) {
std::cout << "Error! Passwords do not match." "\n";
password1 = get_input <std::string>("Please enter your password.");
password2 = get_input <std::string>("Please re-enter your password.");
}
return password1;
}
std::string get_username()
{
std::string username = get_input <std::string>("Please enter a username.");
std::cout << "Username: \"" << username << "\"\n";
std::string filename = "accounts\\" + username + ".txt";
if (FILE *f = fopen(filename.c_str(), "r"))
{
fclose(f);
std::cout << "User exists!" << std::endl;
register_user();
}
else
{
while (get_input <int>("Confirm? [0|1]") != 1) {
username = get_input <std::string>("Please enter a username.");
std::cout << "Username: \"" << username << "\"\n";
}
}
return username;
}
void login()
{
std::string username = get_input <std::string>("Please enter your username.");
std::cout << "Username: \"" << username << "\"\n";
std::string filename = "accounts\\" + username + ".txt";
if (FILE *f = fopen(filename.c_str(), "r"))
{
std::string psw = get_input <std::string>("Please enter your password.");
std::ifstream readFile(filename);
std::string p;
readFile >> p;
fclose(f);
if (psw == p)
{
std::cout << "Login successed" << std::endl;
main_menu();
}
else
{
std::cout << "Login failed" << std::endl;
login();
}
}
else
{
std::cout << "No User!" << std::endl;
login();
}
}
void main_menu()
{
int choice = get_input <int>(
"Hello, Would you like to log in or register?" "\n"
"[1] Login" "\n"
"[2] Register" "\n"
"[3] Exit");
switch (choice)
{
case 1:
login();
break;
case 2:
register_user();
break;
}
}
void register_user()
{
std::string username = get_username();
std::string password = get_password();
save_user(username, password);
}
void save_user(const std::string &username, const std::string &password)
{
std::string filename = "accounts\\" + username + ".txt";
std::ofstream writefile(filename);
writefile << password << "\n";
main_menu();
}