-5

I'm currently using stack library on c++ but i don't know how to receive a stack of numbers in a function.. example---->

int main
{
    stack <int> pila1;
    juegoEnsayo(pila1);
}

void juegoEnsayo(/*What is supposed to be here???*/ &unaPila)
{
    unaPila.push(6);
    unaPila.push(9);
    unaPila.push(8);
}
iehrlich
  • 3,572
  • 4
  • 34
  • 43
  • 3
    Read a good introductory C++ book. You can find a list on StackOverflow – Vittorio Romeo Jul 03 '17 at 09:52
  • I tried but i couldn't find anything – LUIS ORTIZ RIVERA Jul 03 '17 at 10:09
  • 4
    You didn't find *anything* ? [**Here.**](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?s=1|32.7215). Found by simply putting `[c++] book` in the search box at the top of this page. And to answer your question, `stack` is what should go there – WhozCraig Jul 03 '17 at 10:10

2 Answers2

1

I suppose stack <int> to be there.

#include <stack>

using namespace std;

void juegoEnsayo(stack<int> &unaPila)
{
    unaPila.push(6);
    unaPila.push(9);
    unaPila.push(8);
}

int main()
{
    stack <int> pila1;
    juegoEnsayo(pila1);
}
Dobby
  • 46
  • 1
  • 5
1

Use stack<int>. So prototype of function will be: void juegoEnsayo(stack<int>& unaPila);

Following is working example. You can find it working here:

#include <stack>
#include <iostream>

using namespace std;

void juegoEnsayo(stack<int>& unaPila)
{
    unaPila.push(6);
    unaPila.push(9);
    unaPila.push(8);
}

template<typename T> void printElm(stack<T> mystack)
{
    while (!mystack.empty())
    {
        cout << mystack.top() << " | ";
        mystack.pop();
    }
}

int main()
{
    stack<int> pila1;
    printElm(pila1);
    cout<<endl;
    juegoEnsayo(pila1);
    printElm(pila1);
    return 0;
}
cse
  • 4,066
  • 2
  • 20
  • 37
  • ok, it works fine , i found the problem why by writting stack wasn't working, i had written the function under the main() one and i had put the declaration over "using namespace std;" i'm definetly a noob ,thanks you all – LUIS ORTIZ RIVERA Jul 05 '17 at 13:29