Help With Stacks

Hello, I'm supposed to be writing a program that sorts the largest elements of a stack with random numbers into a stack called ordered. I believe I have the program correct, however I can't get it to execute the print function at the end or to sort the other stacks?(I actually don't know if it has sorted the stacks seeing as I can't get it to print, yet I'm calling the public function the correct way). Any help would be appreciated, I've included the application file code and the header file code below.

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
#include <cstdlib>
#include <iostream>

#include "stack.h"

using namespace std;

int main(int argc, char *argv[])
{
    stack ordered;
    stack temp1;
    stack temp2;

    int number;
    int item;


    cout << "Enter the number of elements in the stack: ";
	cin >> number;

		for (int i=0; i < number; i++)
	{
		item = rand()%100 + 1;

        temp1.push(item);
	}

	cout<<"Stacks at the start: ";

    ordered.print();
    temp1.print();
    temp2.print();

while(temp1.empty() == false && temp2.empty() == false)
	{
        if(temp2.empty())
	    {
     	stack temp = temp1;

        int largest = temp1.pop();

        while(temp1.empty() == false)
			{
				int element = temp1.pop();

        		if (largest < element)
		  			largest = element;
			}
			while(temp.empty() == false)
			{
				int element = temp.pop();

        		if (element!=largest)
					temp2.push(element);
			}

			ordered.push(largest);
        }
        else
        {
        stack temp=temp2;

        int largest = temp2.pop();

        while(temp2.empty() == false)
		{
			int element = temp2.pop();

        	if (largest < element)
				largest = element;
		}
		while(temp.empty() == false)
		{
			int element = temp.pop();

        	if (element!=largest)
				temp1.push(element);
		}
		ordered.push(largest);
    }

    ordered.print();
    temp1.print();
    temp2.print();

	}

    system("PAUSE");
    return EXIT_SUCCESS;
}



Header File

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
// implementation file for the stack class

#include <iostream>
#include <cstdlib>

using namespace std;

const int stack_size = 1000;

class stack
{
      private:
          int data [stack_size];  // data for the stack
          int top;  // index of the top of the stack

      public:
          stack ();  // creates an empty stack
          void push (int item);  // puts item on the top of the stack
          int pop ();  // removes and returns the top of the stack
          bool full ();  // returns true if the stack is full
          bool empty (); // returns true if the stack is empty
          void print ();
};

// constructor creates an empty stack
stack::stack ()
{
      top = -1;
}

// push adds a new elment, item, to the top of the stack
void stack::push (int item)
{
     // if the stack is full, print an error message
     if (full ())
     {
         cout << "\n\nStack Error: Pushing on a full stack";
         cout << "\nThe element being pushed was: " << item;
     }
     else // OK to push an element
     {
          top++;
          data [top] = item;
     }
}

// pop removes and returns the top element of the stack
int stack::pop ()
{
     // if the stack is empty, print an error message
     if (empty ())
     {
         cout << "\n\nStack Error: Popping an empty stack";
         cout << "\nReturning ?";
         return '?';
     }
     else  // OK to pop
     {
         top--;
         return data [top + 1];
     }
}

// empty returns true if the stack is empty, else it returns false
bool stack::empty ()
{
     return top == -1;
}

// full returns true if the stack is full, else it returns true
bool stack::full ()
{
     return top == stack_size - 1;
}

void stack::print()
{
	int item;
	if (empty())
	{
		cout << "This Stack Is Empty.";
	}
	while(!empty())
	{
		item=pop();
		cout << item << " ";
	}
	cout << endl;
}

You cannot go first time into a while loop
while(temp1.empty() == false && temp2.empty() == false)
because the temp2 is empty by default, hence you cannot have an access to sorting process and final print functions.
And why did you put print functions into a loop? Do you want to print elements every time?
Last edited on
Okay, I had changed that like you said, and it still skipped the while loop and didn't print out at the end(well actually if the while loop was skipped it wouldn't print again because it's inside), but that's besides the point, it's supposed to print each time something is moved from one stack to another.
Every time you are printing an element you're poping it, hence top decreases and your loop work incorrect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void stack::print()
{
	int item;
	if (empty())
	{
		cout << "This Stack Is Empty.";
	}
	long count(-1);
	while (!empty())
	{
		item = pop();
		count++;
		cout << item << " ";
	}
	top = count;
	cout << endl;
}


And get this
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
#include <cstdlib>
#include <iostream>
#include <ctime>

#include "stack.h"

using namespace std;

int main(int argc, char *argv[])
{
	srand(time(NULL));

	stack ordered;
	stack temp1;
	stack temp2;

	int number;
	int item;


	cout << "Enter the number of elements in the stack: ";
	cin >> number;

	for (int i = 0; i < number; i++)
	{
		item = rand() % 100 + 1;

		temp1.push(item);
	}

	cout << "\nStacks at the start:\n";

	cout << "Ordered: "; 
	ordered.print();
	cout << "Temp1: ";
	temp1.print();
	cout << "Temp2: ";
	temp2.print();

	while (temp1.empty() == false || temp2.empty() == false)
	{
		if (temp2.empty())
		{
			stack temp = temp1;

			int largest = temp1.pop();

			while (temp1.empty() == false)
			{
				int element = temp1.pop();

				if (largest < element)
					largest = element;
			}
			while (temp.empty() == false)
			{
				int element = temp.pop();

				if (element != largest)
					temp2.push(element);
			}

			ordered.push(largest);
		}
		else
		{
			stack temp = temp2;

			int largest = temp2.pop();

			while (temp2.empty() == false)
			{
				int element = temp2.pop();

				if (largest < element)
					largest = element;
			}
			while (temp.empty() == false)
			{
				int element = temp.pop();

				if (element != largest)
					temp1.push(element);
			}
			ordered.push(largest);
		}
	}

	cout << "\nOrdered: ";
	ordered.print();
	cout << "Temp1: ";
	temp1.print();
	cout << "Temp2: ";
	temp2.print();

	system("PAUSE");
	return EXIT_SUCCESS;
}
Thank you so much, I truly appreciate your help with this.
Topic archived. No new replies allowed.