GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Jan-2009, 13:04
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

Passing a Stack to modify...


Im not sure if i should be doing this by referance or what... but i want to pass a token *XML tag* to a stack through a function that checks for all my base cases. However i am having trouble passing in the stack and modifying it.. i think its still going out of scope... i just cant remember how to fix that.. i was thinking const stack<string>& stk, but thats if i wanted to modify it without modifying my primary correct? any help woudl be great

CPP / C++ / C Code:
 
stack<string> tagStack;

while (cin >> token){
        if(token[0] != '/0'){
            check = checkTags(token,tagStack);
            cout << tagStack.top();
}

CPP / C++ / C Code:
int checkTags(const string& ONE,stack<string> stk){
    if(ONE != '/0'){
        skt.push(ONE);
    }

// do some more stuff

}
  #2  
Old 16-Jan-2009, 13:09
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Passing a Stack to modify...


Quote:
Originally Posted by TransformedBG
Im not sure if i should be doing this by referance or what... but i want to pass a token *XML tag* to a stack through a function that checks for all my base cases. However i am having trouble passing in the stack and modifying it.. i think its still going out of scope... i just cant remember how to fix that.. i was thinking const stack<string>& stk, but thats if i wanted to modify it without modifying my primary correct? any help woudl be great

CPP / C++ / C Code:
int checkTags(const string& ONE,stack<string> stk){
    if(ONE != '/0'){
        skt.push(ONE);
    }

// do some more stuff

}

You pass by const reference when you don't want the variable to be modified by the function. In your case, just pass it through non-const reference:

CPP / C++ / C Code:
int checkTags(const string& ONE, stack<string> & stk)


Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 16-Jan-2009, 13:14
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

Re: Passing a Stack to modify...


Okay i did that it just isnt modifying the stack outside of the function. stack size not growing
  #4  
Old 16-Jan-2009, 13:19
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Passing a Stack to modify...


Quote:
Originally Posted by TransformedBG
Okay i did that it just isnt modifying the stack outside of the function. stack size not growing

Are you sure your call to push is reached? Step through the code with a debugger or output to the console the branches your program goes through.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 16-Jan-2009, 13:29
TransformedBG TransformedBG is offline
Member
 
Join Date: Oct 2006
Posts: 117
TransformedBG is on a distinguished road

Re: Passing a Stack to modify...


yeah i figured it out i forgot to return an int LOL... wow easy things get me... now i just got to finish working out my base cases and getting it to run correctly. Thanks your a wiz!!!
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Test driver code for stack class glacier C++ Forum 2 03-Oct-2008 04:14
clean up suggestions dabigmooish C++ Forum 4 17-Nov-2006 07:30
prime factors with stack Kubu C++ Forum 5 16-Apr-2006 18:08
Help With Stacks penance C Programming Language 9 10-Oct-2005 09:25
infix to postfix Kacyndra C++ Forum 2 15-Aug-2005 11:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:54.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.