Trouble trying to remove odd numbers from stack

Hello I'm trying to create a stack program that only contains even numbers, basically the program will have some hard coded odd numbers but MUST filter them out and only display the even. This is what I have so far.

Also in my .cpp file I believe it's my int EvenStack::GetevenNumber() function that needs to be adjusted, but i'm not sure how to do that? From reading online i'm thinking it needs something saying it's divisible by 2?

One error i'm receiving in main:
27|error: no matching function for call to 'EvenStack::GetevenNumber(std::stack<int>&)'|

MAIN
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 <stack>
#include "EvenStack.h"
using namespace std;



int main()
{
   EvenStack myStack;

   myStack.push(44);
   myStack.push(66);
   myStack.push(99);
   myStack.push(88);
   myStack.pop();

   stack <int> aStack;
    EvenStack newStack;
    aStack.push(2);
    aStack.push(9);
    aStack.push(6);
    aStack.push(2);
    aStack.push(3);
    aStack.push(5);
    aStack.push(4);
    newStack.GetevenNumber(aStack);
    cout << "aStack has " << aStack.size() << " elements" << endl;
    newStack.emptyStack();

   return 0;
}  


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

#include <string>
#include <iostream>

using namespace std;

// Define the default capacity of the stack
#define MAX 7

class EvenStack
{
   int top;
    public:
        EvenStack();
        EvenStack(int EvenNumber);
        ~EvenStack();


   int myStack[MAX]; //stack array

   Stack() { top = -1; }
   bool push(int x);
   int pop();
   bool emptyStack();

   int GetevenNumber();
   void SetevenNumber(int EvenNumber);

    protected:

    private:
        int m_even_number;
};

#endif // EVENSTACK_H


.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
#include "EvenStack.h"

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

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


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

int EvenStack::GetevenNumber() // The getEvenNumbers fucntion receives a stack of integers (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.
{
    return m_even_number;
}

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

//pushes element on to the stack
   bool EvenStack::push(int item)
   {
      if (top >= (MAX-1)) {
      cout << "Stack Overflow!!!";
      return false;
   }
else {
   myStack[++top] = item;
   cout<<item<<endl;
   return true;
   }
}

//removes or pops elements out of the stack
int EvenStack::pop()
{
   if (top < 0) {
      cout << "Stack Underflow!!";
      return 0;
   }
else {
       int item = myStack[top--];
      return item;
   }
}

//check if stack is empty
bool EvenStack::emptyStack()
{
   return (top < 0);
}
Last edited on
A comment mentioned using the STL stack, so do you really need to implement a whole stack yourself?

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
#include <iostream>
#include <stack>
using namespace std;

// Does everything the regular stack does, except it only pushes even numbers.
class EvenStack : public stack<int> {
public:
  void push(const int &v) {
    if ( v % 2 == 0 ) {
      stack<int>::push(v);
    }
  }
};

int main()
{
  EvenStack myStack;
  myStack.push(44);
  myStack.push(66);
  myStack.push(99);
  myStack.push(88);

  while (!myStack.empty())
  {
     cout << ' ' << myStack.top();
     myStack.pop();
  }
  cout << '\n';
}

A comment mentioned using the STL stack, so do you really need to implement a whole stack yourself?
yes.

: The getEvenNumbers function receives a stack of integers (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. For example, given the stack { 4, 5, 3, 2, 6, 9, 2 } (where 4 is at the top of the stack), the function pushes { 4, 2, 6, 2 }. The initial stack should retain its original values when the function ends.

I may have explained it wrong so I just sent the whole prompt, i'll put my updated .cpp file. From my understanding that ^^ needs to be put in my: int EvenStack::GetEvenNumbers() function.

.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
#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 fucntion receives a stack of integers (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.
    return m_even_number;
}

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

int EvenStack::emptyStack()
{
    stack <EvenStack> myStack;
    while(!myStack.empty())
    {
        cout << myStack.top().GetEvenNumbers() << " "
        << endl;
        myStack.pop();
    }
}




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

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

using namespace std;




class EvenStack

{

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

   int emptyStack(); // empty stack declaration

    int size();
    stack <int> aStack;
    int push();




   int GetEvenNumbers();
   void SetEvenNumbers(int EvenNumber);

    protected:

    private:
        int m_even_number;


};

#endif // EVENSTACK_H 
Last edited on
Topic archived. No new replies allowed.