Reversing Queue Condensed Version() {

// Need to input multiple integers and get them returned in reversed order.  This code does not print in reverse.

#include <iostream>
#include <stack>

using namespace std;


int main(void)
   {
   stack<int> IntStack;
   int Num;

   cout << "Enter an integer (or CTRL z to end data entry): ";
   cin >> Num;

   while(! cin.fail())
      {
      IntStack.push(Num);
      cout << "Enter an integer (or CTRL z to end data entry): ";
      cin >> Num;
      }

   cout << endl << "The numbers in reverse order are:" << endl;
   while (! IntStack.empty())
      {
      Num = IntStack.top();
      IntStack.pop();
      cout << Num << endl;
      }

   return 0;
   }

edelossantosAsked:
Who is Participating?
 
waysideConnect With a Mentor Commented:
???

This code worked fine for me:

Enter an integer (or CTRL z to end data entry): 1
Enter an integer (or CTRL z to end data entry): 3
Enter an integer (or CTRL z to end data entry): 5
Enter an integer (or CTRL z to end data entry): 7
Enter an integer (or CTRL z to end data entry): 9
Enter an integer (or CTRL z to end data entry): ^Z

The numbers in reverse order are:
9
7
5
3
1
0
 
edelossantosAuthor Commented:
[edeloss2@pegasus exp]$ a.out
Enter an integer (or CTRL z to end data entry): 1
Enter an integer (or CTRL z to end data entry): 2
Enter an integer (or CTRL z to end data entry): 3
Enter an integer (or CTRL z to end data entry): 4
Enter an integer (or CTRL z to end data entry): 5
Enter an integer (or CTRL z to end data entry): 6
Enter an integer (or CTRL z to end data entry): 7
Enter an integer (or CTRL z to end data entry): 8
Enter an integer (or CTRL z to end data entry):
[1]+  Stopped                 a.out

// why am I getting this output?
0
 
waysideCommented:
Your shell is set up to suspend the process when you press control-Z.

I found I could press almost any control character such as control-A to break out of the input loop. Try different ones, if none work for you, I would rework the input loop to look for a certain character such as a . (period) to break out of the input loop.

You could also change the suspend character of your shell, but that seems a little drastic.

0
All Courses

From novice to tech pro — start learning today.