Why does my pop function in my .cpp file not erase the last element from my stack?

I updated the post, please scroll down and check out my issue. Help is very much appreciated. Below is the .cpp, main and .h



Last edited on
Okay I'm finally getting somewhere lol. What I don't understand is why it says "error - can only push even numbers" three times and "Removing the following: 3x"? .

I'm also trying to pop the first number off the output so that it only displays 66 & 44 My output is now:

 Error - can only push even numbers
Removing the following:  Error - can only push even numbers
Removing the following:  Error - can only push even numbers
Removing the following:   88 66 44



UPDATED .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

main
[code]

/*
    Test program for Program 
*/
#include <iostream>
#include <stack>
#include "EvenStack.h"

using namespace std;

int main()
{
    EvenStack myStack;
    myStack.push(44); // push this element onto myStack.
    myStack.push(66);
    myStack.push(99);
    myStack.push(88);
    myStack.pop();
    myStack.emptyStack();



    return 0;
}

#include "EvenStack.h"

EvenStack::EvenStack()
{
m_even_number = 0;
}

EvenStack::EvenStack (int EvenNumber)
{
m_even_number = EvenNumber;
}


EvenStack::~EvenStack()
{
//dtor
}

int EvenStack::getEvenNumbers() // The getEvenNumbers function receives a stack of integers(myStack) (implemented in the by STL stack)
{ // and pushes all of the even numbers from the input stack (preserving their original order) onto the EvenStack object(aStack).
// stack of integers
if (InNum % 2 == 0)
{
aStack.push(InNum);
}


}

void EvenStack::setEvenNumbers(int EvenNumber)
{
m_even_number = EvenNumber;
}

int EvenStack::emptyStack()
{
while (!myStack.empty())
{
cout << ' ' << myStack.top();
myStack.pop();
}
}

void EvenStack::push(int EvenNumber)
{
if ( EvenNumber % 2 == 0 ) {
myStack.push(EvenNumber);

std::cout << "Error - can only push even numbers" << endl << "Removing the following: ";
while (!aStack.empty () )
{
std::cout<<" " << myStack.top ();
myStack.pop();
}
}
}

void EvenStack::pop()
{
while (!myStack.empty () )
{
std::cout <<" " << myStack.top();
myStack.pop();
}
std::cout<<"\n";

}
[/code]

.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
#ifndef EVENSTACK_H
#define EVENSTACK_H

#include <stack>
#include <string>
#include <iostream>

using namespace std;




class EvenStack

{

    public:
        EvenStack();
        EvenStack(int EvenNumber);
        ~EvenStack();

        stack <int> aStack, myStack;         // aStack is the even numbers, myStack is all the numbers.


    int emptyStack(); // empty stack declaration
    int size();
    void top();
    void push(int EvenNumber);
    void pop();
    int MyNumbers[7];
    int InNum;





   int getEvenNumbers();
   void setEvenNumbers(int EvenNumber);

    protected:

    private:
        int m_even_number;


};

#endif // EVENSTACK_H
Last edited on
Can someone help I don't understand why my myStack.pop(); doesn't erase 88 from the output
I sure can't. You did not show the code for pop for the other class, you only show it for evenstack.
pop normally returns the value removed, and you normally do something with it. Popping to destroy something you have never seen is weird, but ok, you can do it that way -- you could read the thing then pop-remove it I guess, but the operations are not normally separate this way.
@jonnin,

pop normally returns the value removed, and you normally do something with it. Popping to destroy something you have never seen is weird, but ok, you can do it that way -- you could read the thing then pop-remove it I guess, but the operations are not normally separate this way.


If I am reading your post correctly:

Actually it goes both ways. I prefer pop() to return the value. However, in a lot of implementations--including the STL--top() (return the top value) and pop() (void return) are separate functions.

1
2
3
4
5
6
7
8
9
10
void EvenStack::pop()
{
  while (!myStack.empty () ) //¿?
  {
    std::cout <<" " << myStack.top(); //¿?
    myStack.pop();
  }
  std::cout<<"\n";

}
explain the two marked lines:
- ¿why do you have a loop?
- ¿why does your function print to screen?

stop copy-pasting code
Topic archived. No new replies allowed.