scope issues

I want line 152 of intstack.cpp to be possible without restructuring my entire program. Is there a way to do this?

intstack.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
#pragma once

#include <stdlib.h>
class IntStack
{
    //int DEFAULT_current_element;
    int current_element;
    int* st_arr;
public:
    const static int DEFAULT_STACK_CAPACITY = 100;
    size_t stack_capacity;
    //default constructor
    IntStack() : /*DEFAULT_STACK_CAPACITY(100),*/ st_arr(new int [DEFAULT_STACK_CAPACITY]), stack_capacity(DEFAULT_STACK_CAPACITY), current_element(0)
    {
    }
    //overloaded constructor
    IntStack(int n) : stack_capacity(n), st_arr(new int [DEFAULT_STACK_CAPACITY]), current_element(0)
    {
    }
    //destructor
	~IntStack() {delete [] st_arr;}
    void reset();
    void push(int n);
    void push(int a[], size_t array_size);
    int pop();
    void pop(int a[], size_t n);
    int top();
    size_t size();
    size_t capacity();
    void double_capacity();
    bool is_full();
    bool is_empty();
    //void evaluate(std::string com);
    friend IntStack print_stack(IntStack* pstack);
};


main.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
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
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cassert>
#include "intstack.h"

using namespace std;

void command_list();
void evaluate(string com, IntStack* st);
IntStack create_stack(char capans, int cap);
int main()
{
    string command;
    //int st_num = 0;

    char capans = 'y';
    int cap;
    cout << "Welcome to the stack program. To begin with, a stack must be created. \n";
    cout << "Would you like to use the default capacity of 100? y/n \n";
    cin >> capans;
    if (capans == 'n'){
        cout << "Enter capacity: \n";
        cin >> cap;
    }
    IntStack st = create_stack(capans, cap);

    int arr_size;
    cout << "Now an array of integers needs to be created. How many integers will this array have? \n";
    cin >> arr_size;
    int arr[arr_size];
    int el;
    for (int i = 0; i < arr_size; i++){
        cout << "Enter element " << i << " of the array \n";
        cin >> el;
        arr[i] = el;
    }

    cout << "Here are your command choices: \n";
    cout << "push     //to push ints to stack \n";
    cout << "pop      //to pop off elements \n";
    cout << "top      //to get top element of stack \n";
    cout << "print    //to print a stack \n";
    cout << "size     //to get the size of a stack \n";
    cout << "capacity //to see the capacity fo a stack \n";
    cout << "reset    //reset stack \n";
    cout << "commands //to display this command list \n";
    cout << "exit     //to exit the program \n\n";
    //int result;
    while(1==1){
        cout << "> ";
        cin >> command;
        evaluate(command, &st);

    }
    return 0;
}


intstack.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
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
#include <string>
#include "intstack.h"
#include <iostream>

using namespace std;

IntStack print_stack(IntStack* pstack){
    for (int i = 0; i < pstack->current_element; i++){
        cout << pstack->st_arr[i] << "\n";
    }
}

int IntStack::top(){
    current_element--;
    int temp = current_element;
    current_element++;
    return st_arr[temp];
}

size_t IntStack::size(){
    return current_element;
}

size_t IntStack::capacity(){
    return stack_capacity;
}

void IntStack::reset(){
    for (int i = 0; i < stack_capacity; i++){
        st_arr[i] = NULL;
    }
    current_element = 0;
}

void IntStack::push(int n){
    if (current_element == stack_capacity){
        cout << "Stack full... doubling capacity \n";
        double_capacity();
    }
    st_arr[current_element] = n;
    current_element++;
}

void IntStack::push( int arr[], size_t array_size ){
    for (int i = 0; i < array_size; i++){
        if (current_element == stack_capacity){
            double_capacity();
        }
        push(arr[i]);
    }
}

int IntStack::pop(){
    if (current_element == 0){
        cerr << "STACK EMPTY\n";
    }
    --current_element;
    int temp = st_arr[current_element];
    st_arr[current_element] = NULL;
    return temp;
}

void IntStack::pop( int a[], size_t n ){
    if (current_element > 0)
        current_element--;
    for (int idx = 0; idx < n; idx++){
        if (current_element == 0){
            cerr << "STACK EMPTY\n";
        }
        a[idx] = st_arr[current_element];
        st_arr[current_element] = NULL;
        if(current_element>0)
            current_element--;
    }
    //cout << "after popping, i is " << i << "\n"; //DB
}

int* allocate(int num);
void deallocate(int* old);

void IntStack::double_capacity(){
    int* new_mem = allocate(stack_capacity * 2);

    for (int i = 0; i < current_element; i++){
        new_mem[i] = st_arr[i];
    }
    deallocate(st_arr);
    st_arr = new_mem;
    stack_capacity *= 2;
}

int* allocate(int num){
    int* new_mem = new int[num];
    return new_mem;
}

void deallocate(int* old){
    delete[] old;
}

bool IntStack::is_full(){
    if(current_element == 100)
        return true;
    else
        return false;
}

bool IntStack::is_empty(){
    if(current_element != 0)
        return false;
    else
        return true;
}

//-------------------------
//---Interface Functions---
//-------------------------

void command_list(){
    cout << "Here are your command choices: \n";
    cout << "push     //to push ints to stack \n";
    cout << "pop      //to pop off elements \n";
    cout << "top      //to get top element of stack \n";
    cout << "print    //to print a stack \n";
    cout << "size     //to get the size of a stack \n";
    cout << "capacity //to see the capacity fo a stack \n";
    cout << "reset    //reset stack \n";
    cout << "commands //to display this command list \n";
    cout << "exit     //to exit the program \n\n";
}

void evaluate(string com, IntStack* st_ ){
    int c;

    if (com == "push")    { c = 0; }
    if (com == "pop")     { c = 1; }
    if (com == "top")     { c = 2; }
    if (com == "print")   { c = 3; }
    if (com == "size")    { c = 4; }
    if (com == "capacity"){ c = 5; }
    if (com == "reset")   { c = 6; }
    if (com == "commands"){ c = 7; }
    if (com == "exit")    { c = 8; }
    else                  { c = 9; }

    IntStack st = *st_;
    switch (c)
    {
        case 0:
            int x;
            cout << "Enter int: \n";
            cin >> x;
            st.push(x);
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            //print_stack(&st);
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            command_list();
            break;
        case 8:
            exit(1);
        case 9:
            break;
    }
}

IntStack create_stack(char capans, int cap ){
    if (capans == 'y'){
        IntStack st;
        return st;
    }
    if (capans == 'n'){
        IntStack st(cap);
        return st;
    }
}
Last edited on
void evaluate(string com, IntStack &st);
Right under my nose, thanks. Updated.
But why won't any of my commands but "exit" work?
IntStack st = *st_; You are making a copy there.
Just use the pointer. st_->push(x);
Thanks.

It still doesn't output the cout though, and the "command" command doesn't do anything. It was working earlier, but now it's not, and I don't know why.
Last edited on
Topic archived. No new replies allowed.