This is C++ How to Program 7th Edition.

I have to Modify the Package shipping program to include exception handling.

Here is the code please help me modify this i started abit stuck need help exception handling.

// ExceptionHandling.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdexcept>
#include <exception>
using namespace std;
using std::runtime_error;

int _tmain(int argc, _TCHAR* argv[])
{
int totalPay = 0;
int hours = 0;
int avgPay;

cout << "Enter the total pay: ";
cin >> totalPay;
cout << "Enter the hours worked: ";
cin >> hours;

try
{
if (hours == 0)
throw exception();
avgPay = totalPay / hours;
cout << "Your average pay per hour is: $" << avgPay << endl;
}
catch (exception &p)
{
cout << "OOPS!!!!!" << endl;
}


return 0;
}

It looks fine to me. I removed the user input for easier testing and gave two test cases:

#include <iostream>
#include <stdexcept>
#include <exception>
using namespace std;
using std::runtime_error;

int main(int argc, char* argv[])
{
int totalPay = 0;
//int hours = 0; // throws exception
int hours = 1; // doesn't throw exception
int avgPay;

try
{
 if (hours == 0)
 {
  throw exception();
 }
 avgPay = totalPay / hours;
 cout << "Your average pay per hour is: $" << avgPay << endl;
}
catch (exception &p)
{
 cout << "OOPS!!!!!" << endl;
}


return 0;
}

What is the problem?

David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.