Tom
Tom

Reputation: 9

Telling my calculator to not divide by zero

I am making a calculator in C++ and it works fine except when I divide by zero. So I have an if statement saying to give the message "Error Divide buy zero" when a number is divide by zero. However the program still crashes.

Here is my code

#include <iostream>

using namespace std;

int main()
{
    int a, b, d, s;
    d == a/b

    cout << "Enter first number\n";
    cin >> a;
    cout << "Enter second number\n";
    cin >> b;
    cout << "Select the operation you want\n";
    cout << "1.Addition\n";
    cout << "2.Subtraction\n";
    cout << "3.Multiplication\n";
    cout << "4.Division\n";
    cin >> s;

    switch (s)
    {
    case 1: cout << "Addition Selected\n"; a+b << endl;    break;
    case 2: cout << "Subtraction Selected\n"; a-b << endl; break;
    case 3: cout << "Addition Selected\n"; a*b << endl;    break;
    case 4:
        if (b==0)
        {
            cout << "Error Divide by Zero"<< endl;
        }
        else
        {
            cout << d << endl;
        }
    break;
    default: cout << "Invalid Selection" << endl;
    }

    return 0;
}

On a secondary note when I select an operation it doesn't say that it was selected.

Upvotes: 0

Views: 2791

Answers (3)

Rudi Cilibrasi
Rudi Cilibrasi

Reputation: 885

Your line

d == a/b;

Needs to be

d = a/b;

and it needs to move within the "else" clause. Otherwise the division by 0 error occurs before the print explaining the error.

You are also missing a print for the divide case and should add that.

Upvotes: 1

havogt
havogt

Reputation: 2822

There are many typos/syntax errors in your code.

Here is a working solution close to your code:

int main()
{
    int a, b, d, s;

    cout << "Enter first number\n";
    cin >> a;
    cout << "Enter second number\n";
    cin >> b;
    cout << "Select the operation you want\n";
    cout << "1.Addition\n";
    cout << "2.Subtraction\n";
    cout << "3.Multiplication\n";
    cout << "4.Division\n";
    cin >> s;
    switch( s )
    {
    case 1:
        cout << "Addition Selected\n" << a + b << endl;
        break;
    case 2:
        cout << "Subtraction Selected\n" << a - b << endl;
        break;
    case 3:
        cout << "Multiplication Selected\n" << a * b << endl;
        break;
    case 4:
        if( b == 0 )
        {
            cout << "Error Divide by Zero" << endl;
        }
        else
        {
            d = a / b;
            cout << d << endl;
        }

        break;
    default:
        cout << "Invalid Selection" << endl;
    }
    return 0;
}

Upvotes: 0

Shreevardhan
Shreevardhan

Reputation: 12641

Your code has a lot of syntax errors.

Perhaps this can help you. This smartly avoids divide by zero and prints error message also.

#include <iostream>
using namespace std;

int main() {
    int n1, n2, ans;
    char op;
    cin >> n1 >> op >> n2;
    switch (op) {
    case '+':
        ans = n1 + n2;
        break;
    case '-':
        ans = n1 - n2;
        break;
    case '*':
        ans = n1 * n2;
        break;
    case '/':
        if (n2) {
            ans = n1 / n2;
            break;
        }
    default:
        cerr << "Invalid Input" << endl;
        return 1;
    }
    cout << ans;
    return 0;
}

See http://ideone.com/wa8QJG demo.

Upvotes: 1

Related Questions