Question

In: Computer Science

(In c++ language) 1A) Write a void function GetYear that prompts for and inputs the year...

(In c++ language)

1A) Write a void function GetYear that prompts for and inputs the year the operator was born (type int) from standard input. The function returns the user’s birth year through the parameter list (use pass by reference) unless the user enters an invalid year, in which case a BadYear exception is thrown. To test for a bad year, think about the range of acceptable years. It must be 4 digits (i.e. 1982) and it cannot be greater than the current year (the user obviously cannot be born in a calendar year that has yet to happen!).

1B) Write a try-catch statement that calls the GetYear function defined above. If a BadYear exception is thrown, print an error message and rethrow the exception to a caller; otherwise, execution should just continue as normal.

Solutions

Expert Solution

1.A)  The program is given below. The comments are provided for the better understanding of the logic.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void GetYear(int &year)
{
        //Declare a variable to store the year for temp purpose
        int tempYear;
        //Get the year from the user
        cout << "Enter the Year of birth: ";
        cin >> tempYear;

        //Validate the year
        int length = to_string(tempYear).length();
        //validate for year length = 4
        if (length != 4)
                //If the length is not equal to 4, throw an error back.
                throw "BadYear";
        else
        {
                //Validate if the entered year is greater than the curent year

                //The below block will get the current year in the variable currentYear
                char tempTimeCharVar[50];
                time_t  tempTimeVar = time(NULL);
                errno_t e = ctime_s(tempTimeCharVar, 50, &tempTimeVar);
                
                int timeVarLength = strlen(tempTimeCharVar);
                string tempDateString(tempTimeCharVar);
                int currentYear = stoi(tempDateString.substr(timeVarLength-5));

                //Check if the entered year is greater than the current year.
                if(tempYear > currentYear)
                        //If so, throw error.
                        throw "BadYear";
        }

        //If this code is reached the year entered is fine and that will be assigned to the refernce variable year.
        //The variable year will be accessible in the calling method with the modified value.
        year = tempYear;
}

int main()
{
        int year;
        //Call the GetYear function
        GetYear(year);
        //Print the year returned as reference from the function.
        cout << "The year entered is : " << year << endl;
}


The screenshots of the code and output are provided below.

1.B) The try catch statement can be used as follows.

int main()
{
        int year;
        try
        {
                //Call the GetYear function
                GetYear(year);
                //Print the year returned as reference from the function.
                cout << "The year entered is : " << year << endl;
        }
        catch(exception e)
        {
                //Display the error message and rethrow
                cout << e.what() << endl;
                throw e.what();
        }
}

Related Solutions

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.
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP...
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP ( 0.00 - 1.49 -> fail 1.50 - 4.00 -> pass ) 1b) Write a program in C to display month in Islamic Calendar.
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives an array of 10 integers as a parameter and fills the array by reading the values from a text file (Lab15A.txt). It should also print the values of the array all on the same line, separated by spaces. Write an int function named findLast that receives an array of 10 integers as a parameter and returns the last negative number in the array. Write...
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT