Thread: weird stack output

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by vidioholic View Post
    so you think it is stacking the right way, just not printing right? when i try and pop a queue, it erases one of the objects in the stack. i have another print statement to see what is in the queue i am going to stack. i know that is different. i want to be able to see the numbers in the stack.

    if i try to use
    Code:
    print_struct_queue(s->store[i]);
    it says incompatible type for argument 1 of print_struct_queue
    s->store[i] would have to be of type struct queue, since store is a pointer to such. Did you make sure not to type "struct queue *q" by habit? Otherwise, sure post some code and we'll see what we can see.

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i just have a feeling it has to do with the way the queue is being stacked. lets say we have 2 queues in the stack. if i pop the queue on top, and then try to add to the queue left over, it starts a new queue. it should add it to the left over queue. if i pop once, there is still "something" in the stack and if i pop again it tells me it is empty now.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You didn't need my print function -- you already had print train; I changed to this
    Code:
    void print_station(struct station* s)
    {
        int i;
        for(i=0; i < s->top; i++)
        {
            printf("Train %d: ", i);
            print_train(&(s->store[i]));
            /*printf("%s ", s->store[i]); */
    
        }
            printf("\n");
    
    }
    Everything prints beautifully.

    Other notes: By now you should have realized that free and malloc are in <stdlib.h>. Your add_car and remove_car use q, that is used nowhere else. (In other words, not the trains in the station.)

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, the question is: where do you want the car added? To the train on top of the stack? In that case, you'll need to send &(s->store[s->top-1]) rather than ... something else.

  5. #20
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    why s->top-1?

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because s->top doesn't have anything in it. It always points at where the next train will go, not at the train at the top of the stack.

  7. #22
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    ok i understand. wow its sooo good to see my queue in the stack for once. haha. well what do i do if i want to add a new car to the queue on the top of the stack?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by vidioholic View Post
    ok i understand. wow its sooo good to see my queue in the stack for once. haha. well what do i do if i want to add a new car to the queue on the top of the stack?
    Quote Originally Posted by tabstop View Post
    Well, the question is: where do you want the car added? To the train on top of the stack? In that case, you'll need to send &(s->store[s->top-1]) rather than ... something else.
    Isn't that where we started?

  9. #24
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    ok but i dont understand how to send it to there and attach it to the train. also if i have a train depart, and add to the train under it, it just acts as if it is a new queue all together. should i pop the train and then add the car and push it back on?

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by vidioholic View Post
    ok but i dont understand how to send it to there and attach it to the train.
    Just typing seventeen words in a row does not always lead to a coherent sentence. Take a deep breath and try again.

    also if i have a train depart, and add to the train under it, it just acts as if it is a new queue all together. should i pop the train and then add the car and push it back on?
    Since the part I have highlighted is factually incorrect, I am unable to answer the question.

  11. #26
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    *wheeew* ok

    lets say there are 2 trains in the station. one of them leaves. now with that last train, i want to add a car to it. i am having two problems with this. first one is if i just add_car, it makes a new queue all together. when train_departs runs the print_station function, it prints an empty line. im sure i need a pointer somewhere. i though s->top-- would bring the stack down one and point at the next queue...

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay. I got that to happen, sort of: add_car works fine (I'm assuming you managed at some point to trade off "q" for "&(s->store[s->top-1])"), but the second departure doesn't. Looking at the code again, you've forgotten one detail: the queue q (in main) is completely meaningless. When you're in, say, train_departs, the q that gets passed in has nothing in it. You shouldn't be trying to pop off cars from q, you need to be popping off cars from the train at the top of s. So again, I added
    Code:
    q = &(s->store[s->top-1]);
    to the top of the train_departs thing and now that works.

  13. #28
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    you know what, right before i closed my laptop i noticed that and i tried it. it completely fixed my problem. i wasnt looking at the right queue. thanx for all your help tabstop!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Impix or reverse polish notation
    By P4R4N01D in forum C Programming
    Replies: 10
    Last Post: 11-18-2008, 11:42 PM
  2. weird output
    By kantze in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2006, 12:05 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. float gives weird output
    By Xarr in forum C Programming
    Replies: 4
    Last Post: 05-25-2004, 07:44 PM
  5. HEap and stack, I'm confused
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-31-2002, 10:59 AM