-2

The problem is that if the first action the program calculates (depends on the order of operation) is something divided by 0 (e.g 7/0+3, or 3+7/0), it prints the error message but performs the next action.

Example-

Input: 7/0+2 -----> Output: You can't divide by zero.7/0+2=2

What it should do ----> Output: You can't divide by zero.

It happens because I can't get it to skip the next switch action. If you have an idea or know how o fix it please help. Thank you

P.S If you know a better way to tackle the Order of Operation problem please help.

Shortened code: Just enter something like 7+4/0 or 7-4/0

#include"stdafx.h"
using namespace System;
#include<iostream>
using namespace std;
int main()
{
    long double num1, num2, num3, res;
    char action1, action2;
    cin >> num1 >> action1 >> num2 >> action2 >> num3;
    if ((action2 == '*' || action2 == '/') && (action1 == '-' || action1 == '+'))  //action2 will be preformed before action1 (Order of operation)
    {
        switch (action2)  //I didn't include the options for '+' or '-' because the if statement requires action2 to be '*' or '/'.
        {
        case('/'):
            if(num3==0)
                cout<< "Error, you can't divide by zero";
            else
                res = num2 / num3;
            break;
            default:
                cout << "Input not recognized";
                break;
            }
            switch (action1)
            {
            case('+'):
                cout << num1 << "+" << num2 << action2 << num3 << "=" << res + num1;
                break;
            case('-'):
                cout << num1 << "-" << num2 << action2 << num3 << "=" << res - num1;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
    }
    cout << "\n\n";
    system("PAUSE");
    return 0;
}

Full code:

// Three numbers Calculator
/*Known problems: when the first action is divided by zero the programs prints the error message but runs the next action.
Other than that I think most of the stuff works but I need to check*/
#include "stdafx.h"
using namespace System;
#include<iostream>
using namespace std;
int main()
{
    cout << "Enter action as # to exit program" << endl;
    cout << "Possible actions:+,-,*,/\n" << endl;
    int a = 1; //loop veriable
    while (a == 1) //loop
    {
        long double num1, num2, num3, res;
        char action1, action2;
        cin >> num1 >> action1 >> num2 >> action2 >> num3;
        if ((action2 == '*' || action2 == '/') && (action1 == '-' || action1 == '+'))  //action2 will be preformed before action1 (Order of operation)
        {
            switch (action2)  //I didn't include the options for '+' or '-' because the if statement requires action2 to be '*' or '/'.
            {
            case('/'):
                if (num3 == 0)      //The problem I described at the top occurs here and at another place below
                    cout << "You can't divide by zero.";
                else
                    res = num2 / num3;
                break;
            case('*'):
                res = num2*num3;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
            switch (action1)    //I didn't include the options for '*' or '/' because the if statement requires action1 to be '+' or '-'.
            {
            case('+'):
                cout << num1 << "+" << num2 << action2 << num3 << "=" << res + num1;
                break;
            case('-'):
                cout << num1 << "-" << num2 << action2 << num3 << "=" << res - num1;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
        }
        else //action1 will be performed before action2 (Order of operation)
        {
            switch (action1)
            {
            case('+'):
                res = num1 + num2;
                break;
            case('-'):
                res = num1 - num2;
                break;
            case('/'):
                if (num2 == 0)     //The problem I described at the top occurs here and at anothe place above
                    cout << "You can't divide by zero.";
                else
                    res = num1 / num2;
                break;
            case('*'):
                res = num1*num2;
                break;
            case('#'):
                system("PAUSE");
                return 0;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
            switch (action2)
            {
            case('+'):
                cout << num1 << action1 << num2 << "+" << num3 << "=" << res + num3;
                break;
            case('-'):
                cout << num1 << action1 << num2 << "-" << num3 << "=" << res - num3;
                break;
            case('/'):
                if (num3 == 0)
                    cout << "You can't divide by zero.";
                else
                    res = num2 / num3;
                break;
            case('*'):
                res = num2*num3;
                break;
            case('#'):
                system("PAUSE");
                return 0;
                break;
            default:
                cout << "Input not recognized";
                break;
            }
        }
        cout << "\n\n";
    }
}
Sela12
  • 55
  • 9
  • For order of operation, you might want to see infix to postifx conversion and its evaluation. – Sniper Nov 28 '16 at 11:04

2 Answers2

1

Have a special check for division by zero right after the input (and before the if) and if so just continue the loop.

So something like

while (...)
{
    ...
    std::cin >> ...
    if (action1 == '/' && num2 == 0 || action2 == '/' && num3 == 0)
    {
        std::cout << "Division by zero\n";
        continue;
    }
    if (...)
    ...
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • That's a good idea. I think I will do as you said but instead of using "continue;" I will make this as the first "if" and then make the second one an "else if" – Sela12 Nov 28 '16 at 12:55
0

As you know break only breaks you out of the switch block, not the loop.

If you're keen to avoid a goto (which I would be), then a simple remedy is to code the while loop in a separate function, and use return if the division by zero condition is encountered to exit that function prematurely. Splitting up your code into various functions is a good thing to do anyway.

(I use Boost Spirit - www.boost.org - for my expression parsing in production systems. The learning curve is extremely steep, but well worth it.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Why do you recommend not using "goto"? – Sela12 Nov 28 '16 at 08:21
  • See http://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto Funnily enough your case is one of those where you just *might* consider using a `goto`, but I'd still refactor into a separate function if I were you. – Bathsheba Nov 28 '16 at 08:22