Answer:
#include <iostream>
#include <string>
const std::string USERNAME = "admin";
const std::string PASSWORD = "password";
int main()
{
int attempts = 0;
bool success = false;
while (attempts < 3 && !success)
{
std::string username, password;
std::cout << "Enter username: ";
std::cin >> username;
std::cout << "Enter password: ";
std::cin >> password;
try
{
if (username != USERNAME || password != PASSWORD)
{
throw std::invalid_argument("Invalid username or password");
}
success = true;
}
catch (const std::invalid_argument& e)
{
std::cout << "Error: " << e.what() << std::endl;
attempts++;
}
}
if (success)
{
// Proceed to the program
std::cout << "Access granted" << std::endl;
}
else
{
std::cout << "Access denied" << std::endl;
}
return 0;
}