Question

In: Computer Science

Write C++ code that prompts the user for a username and password (strings), and it calls...

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. Use virtual functions, pure virtual functions, templates, and exceptions wherever possible. Please explain the code.

Your code should print error messages if login() generates an exception:

  • LoginException: "Username not found or password incorrect"
  • ServerException: "The login server is busy and you cannot log in"
  • AccessException: "Your account is forbidden from logging into this server"
  • Any other exception: "Unknown login error occurred: " + ex.what() + "\n

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class Base//base class declared which is storing the required password and username
{
    string user = "ak2783934";     //user name saved here
    string password = "password1"; //password saved here

public:
    virtual void usernameRetrieve() = 0; //pure virtual function
    virtual void passwordRetrieve() = 0; //pure virtual fucntion
    string getUser() { return user; }
    string getPass() { return password; }
};
class Derived : public Base//derived class declared for accessing the forbidden values of passwords and username
{
    string username;
    string password;

public:
    string getUsername() { return username; }//member function for getting username from the base variable
    string getPassword() { return password; }//member function for getting password from the base class
    void usernameRetrieve() { username = getUser(); }
    void passwordRetrieve() { password = getPass(); }
};

void login(string username, string password)
{
    Derived loginDetails;
    loginDetails.usernameRetrieve();//retrieving username
    loginDetails.passwordRetrieve();//retrieving password
    bool userMatched;
    if (username == loginDetails.getUsername()) //if username matched then make usermatched = true
        userMatched = true;
    else
        userMatched = false;

    bool passMatched;
    if (password == loginDetails.getPassword())//if password matched then make the passMatched = true
        passMatched = true;
    else
        passMatched = false;

    try   //exception block 
    {
        if (!userMatched or !passMatched)//if any of them is false then throw an exception
        {
            if (userMatched == false)
                throw userMatched;
            else
                throw passMatched;
        }
    }
    catch (bool userMatched)  //check if exception is thrown with userMatched
    {
        cout << "User not found" << endl;
    }
    catch (bool passMatched)  //check if the expcetion is thrown with passMatched 
    {
        cout << "Password did not match" << endl;
    }
    if (userMatched and passMatched)//if both of them is matched then we print login succesful
    {
        cout << "Login successful" << endl;
    }
}
int main()
{
    cout << "Enter the username: ";//asksing for username
    string username;
    cin >> username;
    cout << "Enter the password: ";//asking for password
    string password;
    cin >> password;
    login(username, password);
}

I declared a base class which is used for storing the details of the user but as we are not using any kind of database so I have not thrown the other exceptions. As the question has asked to prompt the user to enter password and username I have done the same and also using private of the base class the username and password are not exposed to outer code.


Related Solutions

Write C++ code that prompts the user for a username and password (strings), and it calls...
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: LoginException: "Username not found or password incorrect" ServerException: "The login server is busy and you cannot log in" AccessException: "Your account is forbidden from logging into this server" Any other exception: "Unknown...
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
C++: Write a student class for the library, which have the following: Username, password, Maximum number...
C++: Write a student class for the library, which have the following: Username, password, Maximum number of copies that a student is allowed to keep(less than 5), maximum borrow periods(less than 30 days per book), list of copy(array) cpp and h
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Class Instructions You will create a password verification program using C-strings and character testing. The user...
Class Instructions You will create a password verification program using C-strings and character testing. The user will enter a password as a C-string. (Character array). You must test that the password contains at least: One lowercase letter One uppercase letter One number (0-9) The password can contain no spaces You are to add one more requirement to the password. I need help in setting this program up. #include <iostream> #include <cctype> using namespace std; int main() { char input; cout...
C++ Question: write a program that prompts the user for the length and width of a...
C++ Question: write a program that prompts the user for the length and width of a rectangle in inches.  The program then uses functions to compute the perimeter and area of the rectangle and to convert those to meters and square meters respectively. Sample output from one instance of the program is shown below: ```html Welcome to the Foot-To-Meter Rectangle Calculator ================================================= Enter the rectangle length in feet: 2 Enter the rectangle width in feet: 3 The rectangle dimensions are: 0.61...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT