I've never seen an exception error before How should I fix it?

Asked 2 years ago, Updated 2 years ago, 102 views

It's a program that receives a median expression, changes it to a posterior expression, and then calculates it If the number divided by 0 is an exception, we put an exception code The code error is Exception occurred (0x75699962, Project4.exe): Microsoft C++ Exception: double, memory location 0x003DF928. Unhandled exception occurred (0x75699962, Project4.exe): Microsoft C++ Exception: double, memory location 0x003DF928. It pops up like this. What should I do in this case?

#include <iostream>
#include <string>
#include <stack>
#include <sstream>
#include <stdlib.h>
using namespace std;

struct oper {
    intp; // operator priority
    string o; // operator
};

stack<double> num; // number stack
stack<oper> op; // operator stack


void calc() {

    double a, b, result;
    b = num.top();
    num.pop();
    a = num.top();
    num.pop();
    string oper = op.top().o;
    op.pop();


    if (oper == "*")
        result = a * b;
    else if (oper == "/")
    {
        result = a / b;
        if (b == 0)throw b;
    }

    else if (oper == "+")
        result = a + b;
    else if (oper == "-")
        result = a - b;


    // Resaving the result values back to the stack
    num.push(result);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    std::cout.tie(NULL);

        string input;
        getline(cin, input);
        stringstream ss(input);

        // Push to stack according to operator priority
        // 0 : ( )
        // 1 : + -
        // 2 : * /

        string tok;

        while (ss >> tok) {
            // (unconditionally pushes to operator stack
            if (tok == "(") {
                op.push({ 0, tok });
            } If //) comes out, calculate until () comes out
            else if (tok == ")") {
                while (op.top().o != "(")
                    calc();
                op.pop();
            }
            else if (tok == "*" || tok == "/" || tok == "+" || tok == "-") {
                int prior; // operator priority
                if (tok == "*")
                    prior = 2;
                else if (tok == "/")
                    prior = 2;
                else if (tok == "+")
                    prior = 1;
                else if (tok == "-")
                    prior = 1;

                // Calculate until operator priority low comes to top
                while (!op.empty() && prior <= op.top().p)
                    calc();
                // Push operator to stack
                op.push({ prior, tok });
            }
            else // push to the number stack if numeric
                num.push(stod(tok));
        }
        // Calculate remaining operators
        while (!op.empty())
            calc();

        try {
            calc();
        }
        catch(double &b){
            std::cout << "Error : zero division error";
        }
            std::cout << fixed;
            std::cout.precision(2);
            std::cout << num.top();      
        return 0;   
}

c++ data-structure

2022-09-20 20:00

1 Answers

The code was compiled into g++.

// omitted

    if (oper == "*")
        result = a * b;
    else if (oper == "/")
    {
        // I used to... After dividing it, the value of b was verified and throwb was done.
        try{
            if ( b == 0 ) {
                throw b;
            }
            result = a / b;
        }
        catch(double &b) {
            std::cout << "Error : zero division error" << endl;
        }
    }

//omitted

                calc();
                // Push operator to stack
                op.push({ prior, tok });
            }
            else // push to the number stack if numeric
                num.push(stod(tok));
        }

        while (!op.empty()){
            calc();
        }

        std::cout << fixed;
        std::cout.precision(2);
        std::cout << num.top();      
        return 0;   
}

I didn't know much about stringstream because I didn't know much about cpp. You've shoveled a bit

As I checked, there was an error in the existing code while accessing the stack that was already pop and retrieving the data... Therefore, I don't think calling calc() should be more than twice (to be exact, an error occurs when calling a top to an empty stack)

stack<double> num; // Number stack

int main() {
    double b;
    b = num.top();
}

// // 0x8000a8e <main+20>: movsd  xmm0,QWORD PTR [rax] #num.top();

The error occurred in the same location as the above test code.

And regarding the zero division part, the calculator didn't accept anything about the throw...

Of course, it seems necessary to take care of the error even if there is only a closed or closed parenthesis other than this is not the case.

Thank you


2022-09-20 20:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.