d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Passing Stacks Into A Function? > C++
Add Reply New Topic New Poll
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Dec 9 2014 06:10pm
I think im having a major derp moment ( probably from so much studying), but regardless here it goes. Its written terribly, but its more or less so i can understand how the code works/ to study for my programming final, and really has no function whatsoever.

How would i pass a stack into a function?, basically so the function would work?

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

using namespace std;
void func(string &cards);
int main()
{
stack <string> cards;

cards.push("King of Hearts");
cards.push("King of Clubs");
cards.push("King of Diamonds");
cards.push("King of Spades");



func(cards);




}




void func(string &cards)
{
cout << " This is the amount of cards in this bitch : ";
cout << cards.size();
cout << endl;
}



It's for the proof of concept, i know i can just put the
cards.size();
outside of the function and it will work,but im wondering how to make it work by passing it into a function.

This post was edited by Pino38 on Dec 9 2014 06:15pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 9 2014 07:05pm
for starters, i dont think this bitch has a string datatype.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Dec 9 2014 07:30pm
your function is expecting a single string to be passed in.

change void func(string &cards) to void func(stack<string>& cards)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll