Department of Computer Science: Bahria University, Lahore Campus

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Bahria University, Lahore Campus

Department of Computer Science


Lab Journal 13
(Spring 2020)

Course: Object Oriented Programming - Lab Date: _______________


Course Code: CSL-210 Max Marks: 10
Faculty’s Name: Ms. Zupash Awais

Name: _____________________ Enroll No: ___________________ Class:


______________

Objective(s):
Upon completion of this lab session, learners will be able to:
 Exception Handling in C++

Lab Tasks:

Task 1
Write a program that prompts the user to enter a length in feet and inches and outputs the
equivalent length in centimeters. If the user enters a negative number, throw and handle an
appropriate exception and prompt the user to enter another set of numbers.

#include<iostream>
#include<string>
using namespace std;
int main()

int feet = -1;

double total, inch, cent;

while (feet<0 || inch<0){


cout << "Please enter the number of Feet: ";
cin >> feet;
cout << "Please enter the number inch: ";
cin >> inch;

try

if (feet<0)

throw(feet);

if (inch<0)

throw(inch);
%
Enrollment Number: ____________________________

total = (feet * 12) + inch;

cent = total*2.54;

cout << feet << " feet and " << inch << " inch is "

<< cent << " centimeters" << endl;

catch (int feet)

cout << "You entered an invalid number for feet, please


try again." << endl;

catch (double inch)

{
cout << "You entered an invalid number for inch, please try
again." << endl;
}}
return 0;

Task 2
Write a program that prompts the user to enter a person’s date of birth in numeric form such
as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your
program must contain at least two exception classes: invalidDay and invalidMonth. If the
user enters an invalid value for day, then the program should throw and catch an
invalidDayobject. Similar conventions for the invalid values of month and year.

Lab Grading Sheet :


Max
Obtained
Task Mark Comments(if any)
Marks
s
1. 04
2. 06
Total 10 Signature

Note : Attempt all tasks and get them checked by your Lab Instructor.

Page 2 of 2

You might also like