Thread: Accessing Struct Stack

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    64

    Accessing Struct Stack

    If I have declared the following:

    stack <what> name;
    struct what
    {
    int value;
    string value2;
    };

    How do I push value and value2 on to the stack and how do I output the individual contents?

    Thank You

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Initialize struct what's data members before pushing the struct into the stack. You cannot modify an element in a stack.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    but how do initialize the data?

    do i say

    cin >> what.value;
    cin >> what.value2;

    and how do i then push those items on to the stack. basically, what is the syntax?

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by kuphryn
    You cannot modify an element in a stack.
    Incorrect.
    The first element may be modified.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    64
    okay, i want to push values on to the stack. coming from a struct...

    how do i do this? how do push the values and how do i output the values?

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    what w[2] = {{4, string("Hello")}, {5, string("Bye")}};

    name.push(w[0]);
    name.push(w[1]);
    what w1 = name.top();
    name.pop();
    what w2 = name.top();
    name.pop();

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Like queue, stack is designed to be permanent. Yes, there are two version of top() and one of them returns a reference to the element. Nonetheless, I believe you should not attemp to modify an element since you may only modify the top element.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  4. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM