Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<sstream>
  4. #include<streambuf>
  5. #include<cstdlib>
  6. #include<string>
  7. #include<vector>
  8.  
  9. using std::string;
  10.  
  11. string getUserInfoFromFile(const string& filepath);
  12. std::vector<std::string> split(const string& complexString);
  13. bool validate(const string& lhs, const string& rhs);
  14.  
  15. int main() {
  16. int inputCounter = 0;
  17. string login, password;
  18.  
  19. // 1. User Info file reading
  20. auto userinfo = split(getUserInfoFromFile("userinfo.txt"));
  21. auto validLogin = userinfo.at(0); // login
  22. auto validPassword = userinfo.at(1); // passwd
  23.  
  24. // 2. Input login & password
  25. while(true) {
  26. if (inputCounter == 3) {
  27. std::cout << "You enter the wrong password 3 times. Attempts were over." << std::endl;
  28. exit(1);
  29. }
  30.  
  31. std::cout << "Enter username: ";
  32. std::getline(std::cin, login);
  33.  
  34. std::cout << "Enter password: ";
  35. std::getline(std::cin, password);
  36.  
  37. bool isValidLogin = validate(login, validLogin);
  38. bool isValidPassword = validate(password, validPassword);
  39.  
  40. if (isValidLogin && isValidPassword) {
  41. std::cout << "Login and password is correct! Welcome to the system." << std::endl;
  42. break;
  43. }
  44.  
  45. inputCounter++;
  46. }
  47.  
  48. // DEBUG
  49. // std::cout << "Your username is: " << login << " with password: " << password << std::endl;
  50.  
  51. return 0;
  52. }
  53.  
  54. string getUserInfoFromFile(const string& filepath) {
  55. std::ifstream isf(filepath);
  56. if (isf) {
  57. std::stringstream buffer;
  58. buffer << isf.rdbuf();
  59. return buffer.str();
  60. }
  61. return string();
  62. }
  63.  
  64. std::vector<std::string> split(const string& complexString) {
  65. std::vector<std::string> splitString;
  66. std::istringstream iss(complexString);
  67. do {
  68. string sub;
  69. iss >> sub;
  70. splitString.push_back(sub);
  71. } while (iss);
  72.  
  73. return splitString;
  74. }
  75.  
  76. bool validate(const string& lhs, const string& rhs) {
  77. return lhs == rhs;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement