I was recently trying out some code for an calculator, and i found one which worked..
But whatever i tried, this program would go off immediately after showing the answer on console. Please help me on this, i tried my best to actually make it stop. But it wouldnt work...
I'm using Visual Studio for the coding, If it is related to it, then inform me on it
#include <iostream>
#include <string>
#include <cctype>
#include<conio.h>
    int expression();
char token() {
    char ch;
    std::cin >> ch;
    return ch;
}
int factor() {
    int val = 0;
    char ch = token();
    if (ch == '(') {
        val = expression();
        ch = token();
        if (ch != ')') {
            std::string error = std::string("Expected ')', got: ") + ch;
            throw std::runtime_error(error.c_str());
        }
    }
    else if (isdigit(ch)) {
        std::cin.unget();
        std::cin >> val;
    }
    else throw std::runtime_error("Unexpected character");
    return val;
}
int term() {
    int ch;
    int val = factor();
    ch = token();
    if (ch == '*' || ch == '/') {
        int b = term();
        if (ch == '*')
            val *= b;
        else
            val /= b;
    }
    else std::cin.unget();
    return val;
}
int expression() {
    int val = term();
    char ch = token();
    if (ch == '-' || ch == '+') {
        int b = expression();
        if (ch == '+')
            val += b;
        else
            val -= b;
    }
    else std::cin.unget();
    return val;
}
int main(int argc, char **argv) {
    try {
        std::cout << expression();
    }
    catch (std::exception &e) {
        std::cout << e.what();
    }
    return 0;
}
 
     
    