Skip to content

Instantly share code, notes, and snippets.

@hamsternik
Created March 28, 2017 18:30
Show Gist options
  • Save hamsternik/94531e34731563e9a25dbcb515563d7e to your computer and use it in GitHub Desktop.
Save hamsternik/94531e34731563e9a25dbcb515563d7e to your computer and use it in GitHub Desktop.
Small project for the security information class
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment