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 program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
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
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
All Code should be written in C: 1. Write a C program which prompts the user...
All Code should be written in C: 1. Write a C program which prompts the user to enter two integer values. Your program should then print out all numbers between 1 and 1000 that are divisible by both of those numbers. 2. Modify your program from question 1 such that the first 1000 numbers that are divisible by both numbers are printed out, instead of numbers up to 1000. 3. Using dynamic memory, allocate memory for an array of 100...
Write a c++ code that prompts the user to enter three float numbers and print them...
Write a c++ code that prompts the user to enter three float numbers and print them in acsending order
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...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter two inputs: some text and a word. The program outputs the starting indices of all occurrences of the word in the text. If the word is not found, the program should output “not found”. Example1: Input1: my dog and myself are going to my friend Input2: my Output: 0 11 31 Example 2: Input1: Programming is fun Input 2: my Output: not found
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT