0

I have an assignment where I to make a stack based calculator. I know there are other post explaining this, and I have the stack template made. My real problem for some reason or another, I am having trouble reading a string of numbers / char into a stack. At first I tried using a string object, then I tried using a char array. Neither have worked for me. Can anyone give me some tips or examples of how to read a string of char & int into a stack. It would be very helpful. Thanks

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Nick Evan
0

Do you mean a std::stack? You can just push and pop anything in/out it. It's actually quite simple...

#include <iostream>
#include <stack>
#include <string>

using namespace std;

void ShowStack(stack<string> in)
{
    while (!in.empty())
    {
        cout << in.top() << '\n';
        in.pop();
    }
}
int main()
{
    char s1[] = "I'm a char array!";
    string s2= "I'm a std::string!";
    stack<string> my_stack;
    my_stack.push(s1);
    my_stack.push(s2);
    ShowStack(my_stack);
    return 0;
}

You can always use other kinds of stacks then my string-stack in above example.

If you created somesort of stack yourself, you'll have to post the code to get some advice.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.