Thread: simple Struct question

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    19

    Question simple Struct question

    here is the code:

    struct SalesItem
    {
    char upc;
    string name;
    }

    SalesItem itemList[NUM_ITEMS];

    get_item_name(itemList, upc);

    In the function call for get_item_name I pass the value of upc which can be a char A through I. I am supposed to retrieve the matching name (each value of upc has a corresponding name) and print it. So, itemList[0].upc has value 'A' and itemList[0].name has value "corn", and itemList[1].upc is 'B' and itemList[1].name is "candy", and so on. Is this a possible operation with this type of setup? This code is what our professor gave us to start with.

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I doubt your professor would give an assignment that wasn't possible. If you look at the data structure you know that when you find one item you also have found the other.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    19

    update

    I do not have the index number to the array. I only have the value of the char upc, and with that need to match up the value of "name" and print it. If I knew what index in the array the specific upc was at it would be easy.

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Your task is to find the name when given the upc both of which are stored in the same struct you have an array of structures essentailly a list what would you do with a list? If you had a list on paper you would scan each entry looking for the one that matched right? so go through the list compareing each entry to the one you want to find and when you find it output the name.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    19

    manofSteel

    thank you sir, i think that will help me a great deal!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Simple question from a newbee
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-16-2002, 11:23 AM