Question

CSC 252 Week 4 HW Provide code and full projects neatly and in proper form and in the correct hea...

CSC 252

Week 4 HW

Provide code and full projects neatly and in proper form and in the correct header and cpp files. Code must compile, link and run for any credit.This is Microsoft visual Studio C++ and needs to be done correctly and neatly and I need to be able to copy and paste code to visual studio and this is for a grade so if you don't know the answer don't answer please and thank you.

Exception Handling – Chapter 16

  1. Add exception class objects to the Account classes from week 3 assignment, and add try-catch blocks to the code of all member functions where you think they may be needed.
  2. Using exception handling semantics show what exceptions should be thrown from your class above if you try to withdraw more than there is in the account or above the account’s overdraft limit. Add code to throw the exception
  3. Catch the exception thrown in 2 and handle it by printing an appropriate message to the screen informing the user of the exceeding the balance availability or overdraft limit.

Recursion Labs – Chapter 17

  1. Write an simple program that uses recursion to compute the factorial of a number – make sure to throw an exception if the user enters a negative integer or a non integer data type from which to compute the factorial.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

#include <iostream>
#define OverdraftLimit 3500
using namespace std;

class myexception: public exception
{
virtual const char* what() const throw()
{
    return "Your Withdrawal crosses the overdraft Limit\n";
}
} withdrawal;

class Myexception: public exception
{
virtual const char* what() const throw()
{
    return "Your Withdrawal crosses the overdraft Limit\n";
}
} factorial;


////Account class
class Account
{
     float balance;
    public:
    Account(float x)
    {
        balance=x;
    }
    void withdraw(float x)
    {
        try
        {
         if(x >balance )
         {
             float temp=x-balance;
             balance=0;
             if(temp>OverdraftLimit)    //if the withdrawal exceed to the OverdraftLimit then throw exception
             {
                 throw withdrawal;
             }
             else
             {
               
                 cout<<"Your Withdrawal is Successfully Done\n";
             }
         }
        }
        catch (exception& e)         //catch the exception
        {
            cout << e.what() << '\n';
        }
    }
};


//////factorial
int findfact(int n)        //it will return factorial of number n
{
    if(n==1)return 1;
    else return n*findfact(n-1);
}
void facto()
{
    int fact;
    cout<<"enter a number for finding factorial : ";
    cin>>fact;
    //fact=10.7;
    try
    {
      if(cin.fail()||fact<0)              //if number enter by user is negative or non-integer then throw exception
      {
          throw factorial;
      }
      cout<<"factorial: "<<findfact(fact);
    }
    catch (exception& e)                  //catch the exception
    {
        cout<<"You entered invalid number for finding factorial";
    }
}


int main()
{
    Account ob(5000);
    float amount;
    ob.withdraw(6500); //calling withdrawal function for withdrawal
    ob.withdraw(9500);

    facto();

return 0;
}

Add a comment
Know the answer?
Add Answer to:
CSC 252 Week 4 HW Provide code and full projects neatly and in proper form and in the correct hea...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT