Solved

Reversing Queue Condensed Version() {

Posted on 2004-10-21
317 Views
Last Modified: 2010-07-27
// 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;
   }

0
Question by:edelossantos
    3 Comments
     
    LVL 14

    Accepted Solution

    by:
    ???

    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
     

    Author Comment

    by:edelossantos
    [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
     
    LVL 14

    Expert Comment

    by:wayside
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now