fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. template <int attempts>
  5. struct password_entry
  6. {
  7. static bool get_entry(std::string& myusername, std::string& mypassword)
  8. {
  9. std::cout << "Enter your username: ";
  10. std::cin >> myusername;
  11. std::cout << "Enter your password: ";
  12. std::cin >> mypassword;
  13. if (myusername == "veasy62" && mypassword == "12")
  14. {
  15. std::cout << "Access granted veasy62" << std::endl;
  16. return true;
  17. }
  18. if ( attempts > 1 )
  19. std::cout << "Access denied. You have " << attempts - 1 << " attempts remaining\n";
  20. return password_entry<attempts - 1>::get_entry(myusername, mypassword);
  21. }
  22. };
  23.  
  24. template <>
  25. struct password_entry<0>
  26. {
  27. static bool get_entry(const std::string& /*myusername*/, const std::string& /*mypassword*/)
  28. {
  29. std::cout << "Sorry you have ran out of attempts\n";
  30. return false;
  31. }
  32. };
  33.  
  34.  
  35. int main()
  36. {
  37. std::string mypass, myuser;
  38.  
  39. // max 2 attempts
  40. bool access_granted = password_entry<2>::get_entry(myuser, mypass);
  41. if ( access_granted )
  42. std::cout << "Welcome current user";
  43. else
  44. std::cout << "Please call 555-5555 to reset your password";
  45. }
  46.  
  47.  
Success #stdin #stdout 0s 3476KB
stdin
nogood
10
veasy62
12
stdout
Enter your username: Enter your password: Access denied.  You have 1 attempts remaining
Enter your username: Enter your password: Access granted veasy62
Welcome current user