RPN Calculator not working

Hello, cplusplus.com
I made an C++ header named "math.h", and it just solve an simple expression. I don't get any errors, but when I enter an expression, the program stops working.

math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef MATH_H
#define MATH_H
#include "basic_io.h"
typedef std::stack<double> math_stack;
bool char_operator(std::string &input)
{
    std::string ops[] = {"+","-","*","/"};
    for (int i = 0; i < 4; i++)
    {
        if(input == ops[i])
        {
            return true;
        }
    }
    return false;
}

double perform_operation(const std::string& input, math_stack& calc_stack)
{
    double val1, val2, result;
    val2 = calc_stack.top();
    calc_stack.pop();
    val1 = calc_stack.top();
    calc_stack.pop();
    if (input == "+")
    {
    result = val1 + val2;
    }
    else if (input == "-")
    {
    result = val1 - val2;
    }
    else if (input == "*")
    {
    result = val1 * val2;
    }
    else
    {
    result = val1 / val2;
    }
    return result;
}
double simple_expr(std::string expression)
{
    math_stack num_stack;
    double numerical;
    if(std::istringstream(expression) >> numerical)
    {
        num_stack.push(numerical);
    }
    else if(char_operator(expression))
    {
        double returning;
        returning = perform_operation(expression, num_stack);
        return returning;
    }

}



#endif // MATH_H 


math_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include "time.h"
#include "math.h"
using std::cout;
using std::cin;
using std::string;
using std::endl;

int main()
{
    string expression;
    double result;
    cout<<" RPN Calculator - Lucas Henrique, C++\n Hit \'q\' and Enter to quit"<<endl;
    while(true)
    {
    cout<<"> ";
    cin>>expression;
    if(expression == "q")
    {
        std::cout<<" Exiting. Bye!";
        wait(2000);
        return 0;
    }
    else
    {
        result = simple_expr(expression);
        cout<<result<<endl;
    }
}
}


Thanks!
Your simple_expr() function needs to return a double. Specifically, line 49 ends the function yet has no return.

Also, it'd probably be better if you used a single stack for the RPN for the entire program. Every computation, just pop the elements off the stack, perform the operation, then push the result back. Just ensure there's only 1 element left at the end of the expression.
Last edited on
What I need to do in the code (snip, please :3 ) ?
Your logic appears to be flawed in that you seem to be expecting the user to enter the entire expression at once, but when you use the extraction operator on cin you will only parse one word at a time.

If that is your intent, then, as i like red pandas said you need to return something after line 49. In your case, you probably want to return a NaN, but then you have to check in your main function that your result is valid.

We'll be better able to help you if you explain exactly what type of input your calculator is supposed to take.
@yulingo I expect simple expressions, like " 1 - 1 " or "9 + 8 ", but posfix.
EDIT: Sorry, I was using infix; but I still getting errors :( .
Last edited on
RPN input would be:
1
1
-
9
8
+

or
1 1 - 9 8 +

As it is, you cannot do the operation before all the operants are entered.
http://en.wikipedia.org/wiki/Reverse_Polish_notation

Correction:
1 1 9 8 + - *
Last edited on
That's not a valid expression by the way. You're left with 2 separate values at the end. Either add a binary operator or remove the 9 or 8.
Ok, get it. But when I run and type "1 2 +", the output is 1. Help, please!
The math_stack num_stack; of line 45 is lost from expression to expression as it is an automatic variable, and is created with each call. Pass it in by reference from main() to keep it persistent during the life of the execution.

At the bottom of perform_operation you need to push the result back onto stack for correct operation.

ref: http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
The line if(std::istringstream(expression) >> numerical) is always evaluating the istream&, so will probably always be true, even when an operator is present. This seems to work better:
1
2
3
4
5
6
7
  std::istringstream x(expression);
  x >> numerical;
  if (!x.fail())
  {
    num_stack.push(numerical);
    return numerical;
  }
Last edited on
Sorry, but I don't speak english (I'm brasillian), but I haven't found where is the error. Can you (please, remember, I don't understand you 100%) "fix" it and show me the code?
Topic archived. No new replies allowed.