STL functions like push(), pop() etc

Hi,
I am trying to start this school assignment where I have to make a C++ program that can operate like one of those old HP-35 calculators. Basically I have some sort of an array where I will use 4 elements which are like the registers on the calculator (X,Y,Z,T). According to the specs of the project the calculator used reverse Polish notation which is a way of computing sums digitally.

Basically if I want the program to calculate 1 + 1, I enter 1 which goes into the x register and 1 again so that the initial 1 gets pushed up the stack into the Y register and then I hit + and the value of the x register gets replaced with the value 2.

I have been given a small bit of skeleton code to go into the main class but it will not compile. It is not accepting the lines of code containing the STL functions like: push() and pop() etc.

I am unsure if I need to include something that isn't there but if anyone could help get me started here I'd appreciate it:

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
  #include <cstdlib>
#include <iostream>

#include <stack>
using namespace std;

/*
 * 
 */

int main(int argc, char** argv) {
    HPStack stack;
    string line;
    while(getline(cin, line)){
    stringstream expression(line);
    string token;
    while(expression >> token){
    if(isdigit(token[0])){
    stack.push(atof(token.data()));
    }
   
        else if (token == "+"){
            double x =stack.pop();
            double y = stack.pop();
            stack.push(y+x);
            
        }
        else if (token == "-"){
            double x = stack.pop();
            double y  = stack.pop();
            stack.push(y+x);
        }
    }
    }
          

    return 0;
}
what is an HPStack?

perhaps line 12 should read
stack<double> myStack;

and all references to stack become myStack.

a quick websearch for HPStack showed me several other people doing the same assignment, perhaps you got the idea there?
I guess its supposed to be the name of the stack for this particular program in that it is emulating the operations of this old calculator. I tried to substitute your code here only except calling it 'HPstack' instead of 'myStack'. No luck, I get this error message now:

main.cpp:30:36: error: void value not ignored as it ought to be
double x = HPstack.pop();

And prior to that I get this error message regarding line 15:

main.cpp:22:33: error: variable 'std::stringstream expression' has initializer but incomplete type

I didn't get this idea from the internet, it was assigned to me by my university. I'll go have a look and see if I can find some more examples of the same program online if you say that they are there.

main.cpp:30:36: error: void value not ignored as it ought to be
double x = HPstack.pop();

stack.pop() is a void function, you need to take stack.top(); then call pop to get rid of it.

main.cpp:22:33: error: variable 'std::stringstream expression' has initializer but incomplete type

you need this
#include <sstream>

This site has an excellent cpp reference section, see the panel top left of this page.
Topic archived. No new replies allowed.