-
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
-
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.
-
Would this also apply to my question silent?
-
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...
-
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.
-
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.
-
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.
-
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
-
c=Mystack1.top;
Mystack2.push(c);
Mystack1.pop;
in your switch_stack function should read -
c=Mystack1.top();
Mystack2.push(c);
Mystack1.pop();
-
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.