8 Replies - 759 Views - Last Post: 17 February 2015 - 03:13 PM Rate Topic: -----

#1 bgann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-September 14

Exception Handling Program

Posted 17 February 2015 - 01:05 PM

Here is what the exercise is asking me to do: Please write a program as described below: program that reads in two integers (age, social security number). You should write functions that throw an out-of-range exception forage (no negative numbers)SSN (must be a 9-digit integer)
My code is written below:
#include "std_lib_facilities_4.h"

int main(){

int age = 0;
int ssn = 0;

cout << "Please enter the age and social security number desired separated by a space:\n";
cin >> age >>  ssn;
cout << "The age is: " << age << " and corresponding ssn is: " << ssn;

try{
    if(age < 0)
			throw;
		cout << "\nBad input, Your age cannot be negative.";
	return 0; 
}

catch (exception& e) {
    cerr << "error: " << e.what() << '\n'; 
    return 1; 
}
catch (...) {
    cerr << "Oops: unknown exception!\n"; 
    return 2; 
}
return 0;

try {
    if(ssn != 9)
			throw;
		cout << "\nBad input, Your ssn must contain 9 integers.";
	return 0; 
}

catch (exception& e) {
    cerr << "error: " << e.what() << '\n'; 
    return 1; 
}
catch (...) {
    cerr << "Oops: unknown exception!\n"; 
    return 2; 
}
return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Exception Handling Program

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15490
  • View blog
  • Posts: 62,038
  • Joined: 12-June 08

Re: Exception Handling Program

Posted 17 February 2015 - 01:08 PM

What is your question?
Was This Post Helpful? 0
  • +
  • -

#3 bgann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-September 14

Re: Exception Handling Program

Posted 17 February 2015 - 01:15 PM

It does not seem to do what the program wants. It does not stop say there is anything wrong when I enter a negative number for age or when I input a number more than 9 digits. I just haven't the slightest clue what could be going wrong.
Was This Post Helpful? 0
  • +
  • -

#4 bgann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-September 14

Re: Exception Handling Program

Posted 17 February 2015 - 01:20 PM

It keeps printing the age is negative no matter what age I put in, even if it's positive.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15490
  • View blog
  • Posts: 62,038
  • Joined: 12-June 08

Re: Exception Handling Program

Posted 17 February 2015 - 01:21 PM

Stop and look at some of your initial issues. Take the 'if age less than zero'. Perhaps you need to brush up on scope and why braces are important for defining the body of an 'if' statement.

try
{
    if(age < 0)
       throw; // since no { } then this line is the only body of the 'if' condition.
    
	// happens regardless of the if statement
	cout << "\nBad input, Your age cannot be negative.";

	// happens regardless of the if statement
    return 0;
}
catch (exception& e) 
{
    cerr << "error: " << e.what() << '\n';
    return 1;
}

Was This Post Helpful? 2
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5788
  • View blog
  • Posts: 17,697
  • Joined: 25-December 09

Re: Exception Handling Program

Posted 17 February 2015 - 01:25 PM

Do you realize that without braces {} only one line is contained within a control statement?

Look at this snippet:
    if(age < 0)
			throw;
		cout << "\nBad input, Your age cannot be negative.";
	return 0; 


What do you think will happen every time, no matter what age is equal to?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 bgann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-September 14

Re: Exception Handling Program

Posted 17 February 2015 - 01:46 PM

I have added the braces for both cases, the problem must be bigger than that. Correct?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15490
  • View blog
  • Posts: 62,038
  • Joined: 12-June 08

Re: Exception Handling Program

Posted 17 February 2015 - 01:50 PM

What happens when you run it now?
Was This Post Helpful? 0
  • +
  • -

#9 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1862
  • View blog
  • Posts: 6,711
  • Joined: 19-February 09

Re: Exception Handling Program

Posted 17 February 2015 - 03:13 PM

Hi, I think you need to throw something as an identifier.

//#include "std_lib_facilities_4.h"

#include <iostream>

using namespace std;

int AGE_ERROR=10, SSN_ERROR=20;

int main()
{
  int age = 0;
  int ssn = 0;

  cout << "Please enter the age and social security number "
       << "desired separated by a space:\n";

  try
  {
    cin  >> age >>  ssn;

    if(cin.fail())
      throw "input failure";

    cout << "The age is: " << age 
         << " and corresponding ssn is: " << ssn << endl;

    if(age < 0)
      throw 10;

    cout << "age ok" << endl;

    if(ssn != 9)
      throw SSN_ERROR;

    cout << "SSN ok" << endl;


  }
  catch (int e) 
  {
    cerr << "error: " << e << '\n';

    if(e == 10) 
    {
      cout << "Bad input, Your age cannot be negative.";
    }

    if(e == SSN_ERROR) 
    {
      cout << "Bad input, Your SSN must contain 9 integers.";
    }


    return 1; 

  }
  catch (...) 
  {
    cerr << "Oops: unknown exception!\n"; 
    return 2; 
  }


  return 0;

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1