Stacks

This is a discussion on Stacks within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm having trouble with stacks. For some reason I can't use them unless I use the line using namespace ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    16

    Stacks

    Hello, I'm having trouble with stacks. For some reason I can't use them unless I use the line
    using namespace std;
    Is this normal or is this something I am doing wrong. I am also trying to read out whats on a stack in the same order I put it in without changing the stack. I figured the simplest way to do this would be to make another stack and read whats on the first stack as a value parameter in a function. Is my thinking correct. Here is my code. I have some errors that I can't get rid of all dealing with the stack. The first is on the line
    void switch_stack(stack);
    Can I pass a stack as an input parameter or am I doing something wrong. Here is the complete code so far.

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

    using namespace std;

    void switch_stack(stack); //Function to move items from 1 stack to the other.

    int main (void)
    {//Opens Main Program
    char i;
    string reverse;
    stack <char> Mystack;

    cout<<"Enter String to go on stack.\n";
    cin>>reverse;
    getch();

    for (i=0; i<reverse.length(); ++i)
    {
    Mystack.push(reverse[i]);
    cout<<"Character: " <<reverse[i]<<" Was pushed onto the stack\n";
    getch();
    }

    switch_stack(Mystack);

    while (!Mystack.empty())

    {//Opens While Loop

    cout<<Mystack.top();
    Mystack.pop();

    }//Closes While Loop


    return 0;
    }//Closes Main Program



    void switch_stack(stack Mystack1)
    {//Opens Switch_stack Function
    stack <char> Mystack2;
    char j;
    char c;

    while (!Mystack.empty())
    {//Opens for Loop

    c=Mystack.top;
    Mystack2.push(c);
    Mystack.pop;

    }//Closes for Loop

    while (!Mystack2.empty())

    {//Opens While Loop

    cout<<Mystack2.top();
    Mystack2.pop();

    }//Closes While Loop


    }//Closes Switch_Stack Function

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You either need the using namespace std, or you need to prefix the stack with "std::". Search for some tutorials about namespaces, and it will clear it all up.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    Would this also apply to my question silent?
    SilasP

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Thanks, after making the post last night I came across a post that answered the name space question. I still need some help with this program though. Anymore help is appreciated...

  5. #5
    Unregistered
    Guest
    I think I see two problems.

    First: I don't think Mystack is visible to switch_stack() as you are passing by value and giving the parameter passed in the name Mystack1 (which you never use in switch_stack() by the way, so maybe it is a typo). I would try passing a reference to type stack like this:

    void switch_stack (stack & Mystack1)

    and where you have Mystack in switch_stack() change it to Mystack1. This should assure that everything you do to Mystack1 in switch_stack() that the same change is made to Mystack back in main().

    Second: The switch routine you use in switch_stack seems ok as far as it goes, but I think you need to add the lines:

    c = Mystack2.top();
    Mystack.push(c);

    before the line:

    Mystack2.pop();

    in the last while loop of switch_back to complete the reversal of Mystack.

    Good luck.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Alright thanks for the advice, I will implement some of the changes you mentioned and see what happens. One thign I did forget to mention was that the first stack must remain unchanged. Thats why I didn't want ot use a reference parameter. Thanks for the replies and any further help is appreciated.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    I still can't figure out why my program won't compile past this line. What am I doing wrong here

    void switch_stack(stack); //Function to move items from 1 stack to the other.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Heres my new code. It compiles and runs but something is going wrong in my function. Not sure what though. The program just stops at some point and nothing happens. The whole point of the program is to read a string of characters onto a stack then output them in the same order they were put in. I am suppose to do this without changing the first stack. I figured I would make a function with the first stack as a value parameter and pop the stuff off the first stack and onto the second. What am I doing wrong...

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

    using namespace std;

    void switch_stack (stack<char>); //Function to move items from 1 stack to the other.

    int main (void)
    {//Opens Main Program
    char i;
    string reverse;
    stack <char> Mystack;

    cout<<"Enter String to go on stack.\n";
    cin>>reverse;
    getch();

    for (i=0; i<reverse.length(); ++i)
    {
    Mystack.push(reverse[i]);
    cout<<"Character: " <<reverse[i]<<" Was pushed onto the stack\n";
    getch();
    }

    switch_stack(Mystack);

    while (!Mystack.empty())

    {//Opens While Loop

    cout<<Mystack.top();
    Mystack.pop();

    }//Closes While Loop


    return 0;
    }//Closes Main Program



    void switch_stack(stack<char> Mystack1)
    {//Opens Switch_stack Function
    stack <char> Mystack2;
    char j;
    char c;

    while (!Mystack1.empty())
    {//Opens for Loop

    c=Mystack1.top;
    Mystack2.push(c);
    Mystack1.pop;

    }//Closes for Loop

    while (!Mystack2.empty())

    {//Opens While Loop

    cout<<Mystack2.top();
    Mystack2.pop();
    getch();

    }//Closes While Loop


    }//Closes Switch_Stack Function

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    c=Mystack1.top;
    Mystack2.push(c);
    Mystack1.pop;

    in your switch_stack function should read -

    c=Mystack1.top();
    Mystack2.push(c);
    Mystack1.pop();

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    I had just figured that out before you posted Sorensen. Those were my only errors, the program now works great. Thanks for all the help to everybody who replied. I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 08:22 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 02:28 AM
  3. Avioding Stacks
    By LoafOfBread34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 06:20 AM
  4. Dumping singly linked list into 2 stacks.
    By strotee76 in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2004, 06:48 PM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 03:01 AM

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