Click here to Skip to main content
16,013,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I tried to reverse a list using a stack but on running the program crashes. The description of the program is in the prologue below. huff! I am tired by bugs in my programs!
/*Read a sequence of integer type and revers the list.
Input terminates as soon as integer does not exceed its predecesor and the list is printed */
#include <iostream>
#include <stack>
using namespace std;

int main()
{
    int item=1;
    stack<int> numbers; //Stack initialized to zero
    cout<<"Enter the numbers in only increasing order: "<<endl;
    while(item>numbers.top())
    {
    cin>>item;
    numbers.push(item);
    }
    cout<<endl<<endl;
    while(!numbers.empty())
    {
        cout<<numbers.top()<<" ";
        numbers.pop();
    }
}
Posted
Updated 23-Dec-10 3:43am
v4
Comments
Manfred Rudolf Bihy 23-Dec-10 9:44am    
Edited code to clean up opening and closing tags.

From MSDN STD::stack::top[^]: The top function returns the topmost element of the stack. You should ensure that there are one or more elements on the stack before calling the top function.

Although your stack does exist it does not contain any entries, so a call to top() will throw an exception; and since you are not in a try{}/catch{} block your program crashes. If you change your while{} to a do{}/while{} you can ensure that the stack will always have at least one value before doing the test.
 
Share this answer
 
Hi,

As Richard mentioned (in between) add items to the stack before calling top - there was another bug (changed order)

C++
#include <iostream>
#include <stack>

using namespace std;

int main()
{
    int item = 1;
    stack<int> numbers;
    numbers.push(0); // push a first item on the stack or top() will fail
    cout << "Enter the numbers in only increasing order : " << endl;

    while(item > numbers.top())
    {
        numbers.push(item); // changed order!
        cin >> item;        // -"-
    }

    cout<<endl<<endl;
    while(!numbers.empty())
    {
        cout << numbers.top() << " ";
        numbers.pop();
    }
}
 
Share this answer
 
Comments
optimus_prime1 23-Dec-10 9:03am    
Actually, that puts up two elements extra in my reversed list. So, if enter 2 3 4 5 6 7.
I get the output 7 6 5 4 3 2 1 0.
Why not like this:

C++
#include <iostream>
#include <stack>

using namespace std;

int main()
{
    int lastItem = -1; //Since you start with 1 anyway -1 will be smaller from the start in the condition of the while statment
    int item = 1;
    stack<int> numbers;
    
    cout << "Enter the numbers in only increasing order :   " << endl;

    cin >> item;        // Read first number here
    while(item > lastItem) // proceed only if input was larger than last input
    {
        numbers.push(item);       // changed order!
        lastItem = item;        
        cin >> item;
    }

    cout<<endl<<endl;
    while(!numbers.empty())
    {
        cout << numbers.top() << " ";
        numbers.pop();
    }
}


That should do it! Unless of course you'd also wanted to enter negative numbers. But as an added bonus you wont get any extra elements you had not explicitely entered.


Cheers,

Manfred
 
Share this answer
 
v4
Comments
johannesnestler 23-Dec-10 15:05pm    
good one - 5
Have you tried - oh, I don't know - running it under the debugger?
 
Share this answer
 
Comments
optimus_prime1 23-Dec-10 7:40am    
It crahes as soon as run this. (using CodeBlocks)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900