Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<sstream>
- #include<streambuf>
- #include<cstdlib>
- #include<string>
- #include<vector>
- using std::string;
- string getUserInfoFromFile(const string& filepath);
- std::vector<std::string> split(const string& complexString);
- bool validate(const string& lhs, const string& rhs);
- int main() {
- int inputCounter = 0;
- string login, password;
- // 1. User Info file reading
- auto userinfo = split(getUserInfoFromFile("userinfo.txt"));
- auto validLogin = userinfo.at(0); // login
- auto validPassword = userinfo.at(1); // passwd
- // 2. Input login & password
- while(true) {
- if (inputCounter == 3) {
- std::cout << "You enter the wrong password 3 times. Attempts were over." << std::endl;
- exit(1);
- }
- std::cout << "Enter username: ";
- std::getline(std::cin, login);
- std::cout << "Enter password: ";
- std::getline(std::cin, password);
- bool isValidLogin = validate(login, validLogin);
- bool isValidPassword = validate(password, validPassword);
- if (isValidLogin && isValidPassword) {
- std::cout << "Login and password is correct! Welcome to the system." << std::endl;
- break;
- }
- inputCounter++;
- }
- // DEBUG
- // std::cout << "Your username is: " << login << " with password: " << password << std::endl;
- return 0;
- }
- string getUserInfoFromFile(const string& filepath) {
- std::ifstream isf(filepath);
- if (isf) {
- std::stringstream buffer;
- buffer << isf.rdbuf();
- return buffer.str();
- }
- return string();
- }
- std::vector<std::string> split(const string& complexString) {
- std::vector<std::string> splitString;
- std::istringstream iss(complexString);
- do {
- string sub;
- iss >> sub;
- splitString.push_back(sub);
- } while (iss);
- return splitString;
- }
- bool validate(const string& lhs, const string& rhs) {
- return lhs == rhs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement