I have a problem with my program

Hello, this is my first post. I am making a calculator using stacks. curpos is the the variable of the top of the stack that is initially set to -1. Every time I call the push function, curpos will increment by 1. Then the function will store in the number the user entered. The pop function will decrease by 1 and "pop" the number of curpos out. These 2 functions work fine. The part where I don't get is my add function. The add function is from the RPN class, which is an inherited class of the Stack class. When I call my add function, it will go to the "else" part and displays an error. It goes to else because apparently my curpos is -1(I checked with cout << curpos). However, i am sure that i set my curpos to 1 after my program prompted the user to enter 2 numbers. Since I do not get why my curpos is set back to -1 when my push and pop functions work perfectly, is there something wrong with my inherited class?

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define SIZE 20

void displayerror();

class Stack { //Declare a class
protected:
int curpos;
float value;
float stack[SIZE];
public:
Stack() {
curpos = -1;
}
void push(float);
float pop();
int size();
void show();
};

class RPN: public Stack {
public:
RPN(){};
~RPN(){};
float add();
float subtract();
float multiply();
float divide();
private:
};

void Stack::push(float num) { //Push function
if (curpos >= SIZE)
cout << "stack overflow" << endl;
else {
curpos++;
stack[curpos] = num;
value = stack[curpos];
//cout << value << endl;
}
}

float Stack::pop() { //Pop function
if (curpos != -1) {
curpos--;
return value;
}
else {
cout << "Error Empty Stack" << endl;
return -1;
}
//cout << size() << endl;
}

int Stack::size() { //Returns the postion
return curpos;
}

void Stack::show() { //Shows stack size and the corresponding character
cout << value << endl;
}

float RPN::add() {
//cout << size() << endl;
if(size() > 0) {
push(pop() + pop());
//return stack[curpos];
}
else {
displayerror();
}
}

float RPN::subtract() {
if(size() > 0) {
push(0 - pop() + pop());
}
else {
displayerror();
}
}

float RPN::multiply() {
if(size() > 0) {
push(pop() * pop());
}
else {
displayerror();
}
}

float RPN::divide() {
if(size() > 0) {
push((1 / pop()) * pop());
}
else {
displayerror();
}
}

int main() {
Stack stk;
RPN calc;
char op[5];
char input[50];
float num1, num2;

while(true) { //Loop perpetuates until user chooses to quit
cout << "Type in anykey except q to continue" << endl;
cin >> input;
if (input[0] == 'q')
break;
cout << "Type in the number, the operator, and the number" << endl;
cin >> num1 >> op >> num2;
cin.ignore();
stk.push(num1);
stk.push(num2);

switch(op[0]) {
case '+': calc.add();
break;
case '-': calc.subtract();
break;
case '*': calc.multiply();
break;
case '/': calc.divide();
break;
default: cout << "Unacceptable input" << endl;
break;
}
stk.show();
}
}

void displayerror() { //Displays error message
cout << "ERROR: Not enough items " << endl;
}
Last edited on
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define SIZE 20

void displayerror();

class Stack   //Declare a class
{
protected:
    int curpos;
    // float value; // **** we don't really need this; we can use stack[curpos]

    float stack[SIZE];
public:
    Stack()
    {
        curpos = -1;
    }
    void push(float);
    float pop();
    int size() const ; // **** made const-correct
    void show() const ; // ****
};

class RPN: public Stack
{
public:
    RPN() {};
    ~RPN() {};
    void /*float*/ add();
    void /*float*/ subtract();
    void /*float*/ multiply();
    void /*float*/ divide();
private:
};

void Stack::push(float num)   //Push function
{
    if (curpos >= SIZE)
        cout << "stack overflow" << endl;
    else
    {
        curpos++;
        stack[curpos] = num;
        // value = stack[curpos];
//cout << value << endl;
    }
}

float Stack::pop()   //Pop function
{
    if (curpos != -1)
    {
        const float value = stack[curpos] ; // ****
        curpos--;
        return value;
    }
    else
    {
        cout << "Error Empty Stack" << endl;
        return -1;
    }
//cout << size() << endl;
}

int Stack::size() const // ****  //Returns the postion
{
    return curpos;
}

void Stack::show()  const // **** //Shows stack size and the corresponding character
{
    cout << stack[curpos] /*value*/ << endl;
}

void /*float*/ RPN::add()
{
//cout << size() << endl;
    if(size() > 0)
    {
        push(pop() + pop());
//return stack[curpos];
    }
    else
    {
        displayerror();
    }
}

void /*float*/ RPN::subtract()
{
    if(size() > 0)
    {
        // push(0 - pop() + pop()); // **** the pops are are indeterminably-sequenced
                         // see: http://en.cppreference.com/w/cpp/language/eval_order

        const float a = pop() ;
        const float b = pop() ;
        push(b-a) ;
    }
    else
    {
        displayerror();
    }
}

void /*float*/ RPN::multiply()
{
    if(size() > 0)
    {
        push(pop() * pop());
    }
    else
    {
        displayerror();
    }
}

void /*float*/ RPN::divide()
{
    if(size() > 0)
    {
        // push((1 / pop()) * pop());  // **** the pops are are indeterminably-sequenced
                         // see: http://en.cppreference.com/w/cpp/language/eval_order

        const float a = pop() ;
        const float b = pop() ;
        push(b/a) ;
    }
    else
    {
        displayerror();
    }
}

int main()
{
    // Stack stk; // **** we want to use the RPN object
    RPN calc;

    // char op[5]; // ****
    // char input[50]; // ****

    char op ; // **** we want to read just one character fpr op
    char input ; // **** we want to read just one character fpr input
    float num1, num2;

    while(true)   //Loop perpetuates until user chooses to quit
    {
        cout << "Type in anykey except q to continue" << endl;
        cin >> input;
        if( input == 'q' ) // **** // if (input[0] == 'q')
            break;
        cout << "Type in the number, the operator, and the number" << endl;
        cin >> num1 >> op >> num2;
        cin.ignore();

        calc.push(num1) ; // **** // stk.push(num1);
        calc.push(num2) ; // **** // stk.push(num2);

        switch(op) // switch(op[0])
        {
        case '+':
            calc.add();
            break;
        case '-':
            calc.subtract();
            break;
        case '*':
            calc.multiply();
            break;
        case '/':
            calc.divide();
            break;
        default:
            cout << "Unacceptable input" << endl;

            calc.pop() ; // ***** remove the incorrect value from the stack
            calc.pop() ; // ***** remove the incorrect value from the stack

            break;
        }
        calc.show() ; // **** // stk.show();
    }
}

void displayerror()   //Displays error message
{
    cout << "ERROR: Not enough items " << endl;
}
Thank you so much!
Topic archived. No new replies allowed.