Question

In: Computer Science

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 the current year (the user obviously cannot be born in a calendar year that has yet to happen!).

Then 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.

Consider the Fraction class that we just wrote. Implement an exception handling design in the parameterized constructor to catch zeros in the denominator. Show the new exception class you will need to put in the header (specification) file. Show the definition for your parameterized constructor that throws the exception if the denominator is zero. Finally, show how to use the try-catch statement in the driver program when instantiating a new parameterized Fraction object. (For this part you do not need to show entire programs, only the requested lines of source code.)

Solutions

Expert Solution

GetYear : If you see the function abort on wrong input, don't worry, that is what the question wants.

#include <iostream>
using namespace std;
class BadYear{
};

void getYear()
{
    int year;
        cout << "Enter birth year : ";
        cin >> year;
        if (year<1000 || year>2020)
            throw BadYear();
}
int main()
{
    try
    {
        getYear();
    }
    catch(BadYear e)
    {
        cout << "Invalid year entered\n";
        throw;
    }
    return 0;
}

Fraction:

#include <iostream>
using namespace std;
class ZeroDen{
    ;
};
class Fraction
{
    public:
    Fraction(int num,int den) 
    { 
        ZeroDen zd1;
        if (den == 0) 
            throw zd1; 
        else // continue with the code you wrote in the previous assignment
            cout<<(float(num)/den); // just for example, replace with your own code
        
    }   
        
};

int main()
{
    try
    {
        Fraction fr1(2,0);
    }
    catch(ZeroDen d1)
    {
        cout << "Exception caught : Zeroes not allowed in denominator ";
    }
    
}

Related Solutions

(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...
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
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.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT