In: Computer Science
Write C++ code that prompts the user for a username and password (strings), and it calls the login() function with the username and password as arguments. You may assume that username and password have been declared elsewhere.
Your code should print error messages if login() generates an exception:
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Assuming variables username and password have been declared somewhere, and login method exists, and each and every exception class mentioned in the question is created somewhere.
//asking, reading username and password
cout<<"Enter username: ";
cin>>username;
cout<<"Enter password: ";
cin>>password;
//inside try catch block, trying to login
try{
login(username,password);
//if no exception occurred, displaying a message
cout<<"successfully logged in!"<<endl;
}catch(LoginException ex){
//LoginException has occurred
cout<<"Username not found or password incorrect"<<endl;
}catch(ServerException ex){
//ServerException has occurred
cout<<"The login server is busy and you cannot log in"<<endl;
}catch(LoginException ex){
//LoginException has occurred
cout<<"Your account is forbidden from logging into this server"<<endl;
}catch(exception ex){
//any other exception that inherits exception class has occurred
cout<<"Unknown login error occurred: " << ex.what()<<endl;
}