What am i doin wrong?!

This is a discussion on What am i doin wrong?! within the C++ Programming forums, part of the General Programming Boards category; when i run this code.. the output is all wrong.. can u help me out? the formatting looks all weird ...

  1. #1
    Tara
    Guest

    What am i doin wrong?!

    when i run this code.. the output is all wrong.. can u help me out?
    the formatting looks all weird here.. but when cut and pasting it back to vc++ makes it readable...

    #include <iostream.h>
    #include <string.h>

    // Node class definition:
    template <class Item>
    class Node // Node for use with the Stack class defined below.
    { // Data items stored in a node must have a copy
    public: // constructor.
    Node();
    Node(const Item& data);
    ~Node();

    void setData(const Item& data); // Mutators
    void setNext(Node* next);

    Item getData() const; // Accessors
    Node* getNext() const;

    private:
    Node* _next; // Pointer to the next node.
    Item* _data; // Pointer to the data contained in the node.
    };

    // Default Node constructor:
    template <class Item>
    Node<Item>::Node()
    {
    _next = NULL; // Default is an empty node with no next node.
    _data = NULL;
    }

    // One parameter Node constructor:
    template <class Item>
    Node<Item>::Node(const Item& data)
    {
    _data = new Item(data); // Makes copy of the input data.
    _next = NULL; // No next node by default.
    }

    // Node destructor:
    template <class Item>
    Node<Item>::~Node()
    {
    if (_data != NULL) delete _data; // Give back the memory for data storage.
    }

    // Node mutators:
    template <class Item>
    void Node<Item>::setData(const Item& data)
    {
    if (_data != NULL) delete _data; // Prevent memory leaks.
    _data = new Item(data); // Makes a copy of the input data.
    }

    template <class Item>
    void Node<Item>::setNext(Node* next)
    {
    _next = next;
    }

    // Node accessors:
    template <class Item>
    Item Node<Item>::getData() const
    {
    return *_data;
    }

    template <class Item>
    Node<Item>* Node<Item>::getNext() const
    {
    return _next;
    }

    // Stack class definition:
    template <class Item> // A versatile stack ADT.
    class Stack
    {
    public:
    Stack(); // Default constructor.
    ~Stack(); // Destructor (doesn't need a copy constructor).

    void push(const Item& entry); // Mutators
    Item pop();

    int empty(); // Accessors
    Item peek();

    private:
    Node<Item>* _top; // Data are stored in a sequence of linked nodes.
    };

    // Stack default constructor:
    template <class Item>
    Stack<Item>::Stack()
    {
    _top = NULL; // Empty stack by default.
    }

    // Stack destructor:
    template <class Item>
    Stack<Item>::~Stack()
    {
    Item temp;

    while (_top != NULL)
    {
    temp = pop();
    }
    }

    // Stack push() implementation:
    template <class Item>
    void Stack<Item>:ush(const Item& entry)
    {
    Node<Item>* temp;
    temp = new Node<Item>(entry);
    temp->setNext(_top);
    _top = temp;
    }

    // Stack pop() implementation:
    template <class Item>
    Item Stack<Item>:op()
    {
    Item temp = NULL;
    Node<Item>* next;

    if (_top != NULL)
    {
    temp = _top->getData();
    next = _top->getNext();
    delete _top;
    _top = next;
    }
    else
    {
    cout << "Attempt to pop an empty stack!";
    }
    return temp;
    }

    void main()
    {
    Stack <int> intStack;
    int popInteger, i;
    cout << "processing an integer Stack"<< endl;

    for (i = 0; i < 4; i++){
    intStack.push(i);
    }

    for (i = 4; i>0 ; i--){
    intStack.pop();
    cout<<popInteger<<" popped from Stack"<<endl;
    }
    }

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You're not assigning anything to popInteger, when you're popping your stack the code should be -

    for (i = 4; i>0 ; i--){
    popInteger = intStack.pop();
    cout<<popInteger<<" popped from Stack"<<endl;
    }

    or you could eliminate the popInteger variable completely by outputing the value from intStack.pop() directly -

    for (i = 4; i>0 ; i--){
    cout<<intStack.pop()<<" popped from Stack"<<endl;
    }
    zen

  3. #3
    Tara
    Guest

    thanks

    thank you, so i removed the popInterger variable al ogether as suggeted.. and it works out find.. what i can't figure is upon popping the integers.. there is only 2 int to pop...

    when i increase the int i to see the problem, the intergers to pop are always 1/2 the intergers i pushed in.

    how do i solve that?

    Rolando

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You probably have two pop()'s in your for-loop. Make sure you only have one:
    Code:
    for (i = 4; i>0 ; i--)
       cout<<intStack.pop()<<" popped from Stack"<<endl;
    Last edited by swoopy; 11-29-2001 at 12:58 PM.

  5. #5
    Tara
    Guest

    hmmm

    well now i wanna push and pop characters from a character array..
    this is what i tried but it is not working:

    void main()
    {
    Stack <int> intStack;
    int i;
    char str[10];
    cin>>str;

    for (i = 0; str[i] < 10; i++){
    intStack.push(str[i]);
    }

    for (i = 0; str[i]<10 ;i++){
    cout<<intStack.pop()<<" popped from Stack"<<endl;
    }
    }


    what's wrong here?

    thanks

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Your for loops should be - for (i = 0; i<10 ;i++). However you may want to use strlen to get the amount on characters entered or you'll get unintialised data entered onto you stack if less than 10 characters are entered. Also you might want to use a char stack instead of an int one.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21