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

In c++ language, write a void function GetYear that prompts for and inputs the year the...
In c++ language, 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...
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 LANGUAGE) Write a function called upperLower that inputs a line of text into char array...
(C LANGUAGE) Write a function called upperLower that inputs a line of text into char array s[100]. Output the line in uppercase letters and in lowercase letters This is what I have so far: void * upperLower (const char * s) { char *word[sizeof(s)]; for(int i = 0; i < sizeof(s); i++){ *word[i] = s[i]; } word = strtok(word, " "); int counter = 0; while(word != NULL){ if(counter % 2 == 0){ word[counter] = toupper(word); printf("%s ", word); }...
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.
In C++ language. void printTreeIO(Tnode *n)(3): recursive function that prints out the data in the tree...
In C++ language. void printTreeIO(Tnode *n)(3): recursive function that prints out the data in the tree in order
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.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT