Try/Catch-What's Wrong

This is a discussion on Try/Catch-What's Wrong within the C++ Programming forums, part of the General Programming Boards category; I'm writing a try/catch program that prompts the user to enter a dividend and a divisor. If the user enters ...

  1. #1
    Unregistered
    Guest

    Question Try/Catch-What's Wrong

    I'm writing a try/catch program that prompts the user to enter a
    dividend and a divisor. If the user enters 0 for divisor, display an error message, otherwise display results as shown:

    Output:
    Try / Catch Program
    Written By Angie27

    Enter dividend: 5
    Enter divisor: 0
    Can't divide by this number: 0

    (next entry)

    Enter dividend: 20
    Enter divisor: 10
    20 divide by 10 is 2


    this is my code I keep getting a linker error with the catch(int i)
    how do i fix this code?

    #include <iostream>
    using namespace std;

    int i;
    double divide(double num1, double num2)
    {

    try
    {
    if (num2 == 0) (throw 0);


    }

    catch(int i)
    {
    cout << "Can't divide by this number: ";
    cout << "i" << "\n";
    exit(0);
    }
    return num1 / num2;
    }

    int main()
    {


    double number1, number2;
    cout << "Enter dividend: ";
    cin >> number1;
    cout << "Enter divisor: ";
    cin >> number2;

    divide(number1, number2);
    double quotient = divide(number1, number2);
    cout << number1 << "divided by" << number2 << "is" << quotient;
    cout << "\n";
    return 0;
    }

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    um...

    why don't you do it without the try and catch? It would be simpler.

    #include "iostream.h"

    int main()
    {
    cout<<"\n Enter the numerator number: ";
    double num1;
    cin>>num1;

    cout<<"\n Enter the denominator: ";
    double num2;
    do
    {
    cin>>num2;
    if(num2==0)
    cout<<"\n Re-enter your number:";
    }while(num2==0);

    cout<<"\n The quotient is: ";
    double answer=num1/num2;
    cout<<answer;
    return 0;
    }

  3. #3
    Unregistered
    Guest

    Question try/catch

    I have to use the try/ catch block that's the objective of the program

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It compiles fine with Dev-C++. Try a rebuild all or something, the code looks good.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21