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