Help on understanding Structs

Discussion in 'C Programming' started by Bail, Nov 17, 2005.

  1. Bail

    Bail Guest

    I will have a exam on the oncoming friday, my professor told us that it
    will base upon this program. i am having troubles understanding this
    program, for example what if i want to add all the total calories that
    the user input together. determine which food has the largest calories.
    how do i start to modifiy the program inorder to do the things i listed
    above. thanks

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct {
    char *name;
    int calories;
    int group;
    } FOOD;

    void getFood(FOOD foods[ ], int i ) {
    FOOD newFood;
    void *characters = malloc(81*sizeof(char));

    printf("\nPlease enter the requested information:\n");
    printf(" for food item %d, what is\n", i+1);
    printf(" the name? ");
    gets( characters); // NOTE fgets IS BETTER!
    newFood.name = characters;

    printf(" calories/serving? ");
    scanf("%d", &(newFood.calories));

    printf(" food group? ");
    scanf("%d", &(newFood.group));
    printf("\n");
    foods = newFood;

    } // end getFood // note:a struct is and lvalue !!

    void dump( FOOD foods[ ] , int n ) {
    int i;
    for(i=0; i<n; i++ ) {
    printf("Food %d is: \n", i+1);
    printf(" name : %s\n", foods.name);
    printf(" calories: %d\n", foods.calories);
    printf(" group # : %d\n", foods.group);
    printf("\n");
    } // end for
    } // end dump

    int main( ) {
    int num; // number of foods entered
    int i;
    char dum[81];

    printf("\nHow many foods will you enter? ");
    scanf("%d", &num);

    FOOD foods[num];
    for( i=0; i<num; i++ ) {
    gets(dum); //NOTE: THIS IS HERE BECAUSE OF SCANF
    getFood( foods, i );
    } // end for

    dump( foods, num);


    return 0;
    } // end main
    Bail, Nov 17, 2005
    #1
    1. Advertising

  2. Bail

    vietor Guest

    like this:

    //......
    int total_calories;
    int maxcalories;
    char maxcaloriesname[81]
    //......
    total_calories=0;
    maxcalories=0;
    strcpy(maxcaloriesname,"no");
    //......

    FOOD foods[num];
    for( i=0; i<num; i++ ) {

    getFood( foods, i );
    // .......
    total_calories+=foods.calories;
    if(foods.calories>maxcalories)
    {
    maxcalories=foods.calories;
    strcpy(maxcaloriesname,foods.name);
    }

    } // end for


    //.....
    vietor, Nov 17, 2005
    #2
    1. Advertising

  3. Bail

    Suman Guest

    Bail wrote:
    > I will have a exam on the oncoming friday, my professor told us that it
    > will base upon this program.


    I had whipped up some code, which works, and pasted it only to
    discover, its
    thursday!

    > i am having troubles understanding this
    > program, for example what if i want to add all the total calories that
    > the user input together. determine which food has the largest calories.
    > how do i start to modifiy the program inorder to do the things i listed
    > above. thanks
    >

    [just too many things I felt i had to comment on so ... ]

    1. Don't use gets. Its dangerous. GIYF.
    2. Interactive input with scanf is tricky stuff. Search the archive,
    with something
    like "scanf + string + input + pete ". (pete happens to post some
    code I really like)
    3. Learn about qsort(). Here's some hint: you will need something like
    ....

    int calcmp( const void *l, const void *r )
    {
    const FOOD *left = l;
    const FOOD *right = r;
    if ( left->calories > right->calories )
    return 1;
    /* fill in the blanks ... */
    }

    4. You said you're having trouble. Presumably, not with the code you
    pasted.
    If you understand the code you pasted, the homework should be easy
    :)


    See ya' on friday.
    Suman, Nov 17, 2005
    #3
  4. Bail

    pete Guest

    Suman wrote:

    > 2. Interactive input with scanf is tricky stuff. Search the archive,
    > with something
    > like "scanf + string + input + pete ". (pete happens to post some
    > code I really like)


    That way of using scanf, was primarily promoted by Dan Pop.
    Dan Pop tended to be very accuarate, but was ...

    If I were to say that he had an abrasive personality,
    it would not surprise me in the least,
    if he were to reply with a logical rebuttal
    of how his personality was not abrasive
    and how I must be mentally deficient
    for even thinking something like that.

    --
    pete
    pete, Nov 17, 2005
    #4
  5. Bail

    Suman Guest

    pete wrote:
    [...my ignorance snipped]
    > That way of using scanf, was primarily promoted by Dan Pop.
    > Dan Pop tended to be very accuarate, but was ...


    Hmm. Appears that I just found much the same. But of course,
    post-posting.

    > If I were to say that he had an abrasive personality,
    > it would not surprise me in the least,
    > if he were to reply with a logical rebuttal
    > of how his personality was not abrasive
    > and how I must be mentally deficient
    > for even thinking something like that.
    >


    And I thought I'm the only Ogden Nash fan 'round here.
    Suman, Nov 17, 2005
    #5
  6. Bail

    Bail Guest

    huh? i dont get where to put this code into my program to make it work?
    Bail, Nov 17, 2005
    #6
  7. Bail

    fb Guest

    pete wrote:
    > Suman wrote:
    >
    >
    >>2. Interactive input with scanf is tricky stuff. Search the archive,
    >>with something
    >> like "scanf + string + input + pete ". (pete happens to post some
    >>code I really like)

    >
    >
    > That way of using scanf, was primarily promoted by Dan Pop.
    > Dan Pop tended to be very accuarate, but was ...
    >

    <snip very accurate description> :)

    Indeed...What ever happened to Dan?
    fb, Nov 17, 2005
    #7
  8. Bail

    Bail Guest

    Come on some one help me out here.... dont get this stuffs.
    Bail, Nov 17, 2005
    #8
  9. Bail

    fb Guest

    fb, Nov 17, 2005
    #9
  10. Bail

    Bail Guest

    thanks for the info. is there any more ??? help a poor student here
    Bail, Nov 17, 2005
    #10
  11. Bail

    Default User Guest

    Bail wrote:

    > thanks for the info. is there any more ??? help a poor student here


    What info?



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Nov 17, 2005
    #11
  12. Bail

    Bail Guest

    will someone take the time to explain to me how does the program i
    posted above work? and how do i start modifiy the program inorder to
    calculate the total calories, max calories. ???
    Bail, Nov 18, 2005
    #12
  13. In article <>,
    Bail <> wrote:
    :huh? i dont get where to put this code into my program to make it work?

    You would put it in at the point that you wanted to,
    "add all the total calories that
    the user input together. determine which food has the largest calories."


    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
    Walter Roberson, Nov 18, 2005
    #13
  14. In article <>,
    Bail <> wrote:
    >will someone take the time to explain to me how does the program i
    >posted above work? and how do i start modifiy the program inorder to
    >calculate the total calories, max calories. ???


    What part of it don't you understand? Do you understand basic
    operations with structures? Do you understand the following? If not,
    what *specific* lines do you not understand, and what is your best
    guess about what those lines are supposed to do?


    #include <stdio.h>
    typedef struct { int foodno; float foodcalories } FOOD;

    void describefood( FOOD thefood ) {
    printf( "Food #%d has %f Calories\n", thefood.foodno, thefood.foodcalories );
    }

    void describefoodptr( FOOD *thefoodptr ) {
    printf( "Food #%d has %f Calories\n", thefoodptr->foodno,
    thefoodptr->foodcalories );
    }

    int main(void) {
    FOOD firstfood, secondfood;

    firstfood.foodno = 1;
    firstfood.foodcalories = 1187.3;

    secondfood.foodno = 2;
    secondfood.foodcalories = 831.88;

    describefood( firstfood );
    describefoodptr( &secondfood );
    }
    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
    Walter Roberson, Nov 18, 2005
    #14
  15. Bail

    Bail Guest

    void describefoodptr( FOOD *thefoodptr ) {
    printf( "Food #%d has %f Calories\n", thefoodptr->foodno,
    thefoodptr->foodcalories );

    }


    thefoodptr->foodno???
    Bail, Nov 18, 2005
    #15
  16. Bail

    pete Guest

    [ot]Re: Help on understanding Structs

    Suman wrote:
    >
    > pete wrote:


    > > If I were to say that he had an abrasive personality,
    > > it would not surprise me in the least,
    > > if he were to reply with a logical rebuttal
    > > of how his personality was not abrasive
    > > and how I must be mentally deficient
    > > for even thinking something like that.
    > >

    >
    > And I thought I'm the only Ogden Nash fan 'round here.


    Most of my English text is in free verse,
    and all of my C code is.

    --
    pete
    pete, Nov 18, 2005
    #16
  17. Bail

    Ian Malone Guest

    Bail wrote:

    You cut out
    >> typedef struct { int foodno; float foodcalories } FOOD;

    and other relevant stuff. From a common sig around comp.lang.c atm:
    `Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.'
    It's clearer to others what you mean when you quote the bits you're
    talking about. Anyway...


    > void describefoodptr( FOOD *thefoodptr ) {
    > printf( "Food #%d has %f Calories\n", thefoodptr->foodno,
    > thefoodptr->foodcalories );
    >
    > }
    >
    >
    > thefoodptr->foodno???
    >


    FOOD is a typedef for the struct:
    typedef struct { int foodno; float foodcalories } FOOD;
    An instance of FOOD has members foodno (an integer) and
    foodcalories (a float).

    If you declared an instance like
    FOOD myfood;

    You would access those members with:
    myfood.foodno
    and
    myfood.foodcalories

    The function you are talking about takes a parameter
    (FOOD *thefoodptr)

    So thefoodptr is a pointer to an instance of FOOD.

    You dereference it to get an instance of food like this:
    *thefoodptr /* now an instance of FOOD */
    And would get at the members like this:
    (*thefoodptr).foodno
    and
    (*thefoodptr).foodcalories

    Except this is an awful syntax, so there is a shorthand
    thefoodptr->foodno
    The -> means dereference the object to the left (to hopefully
    get an instance of a struct) then access the member with the
    name on the right.

    --
    imalone
    Ian Malone, Nov 18, 2005
    #17
  18. Bail

    Flash Gordon Guest

    Bail wrote:
    > will someone take the time to explain to me how does the program i
    > posted above work?


    There is no program above. Please read one of the over 1000 messages on
    this group telling you how to provide context using Google. A search for
    the terms "Google context" will find them.

    You cannot assume that people have seen previous messages, because they
    may not have, nor that they will remember them if they have seen them.

    > and how do i start modifiy the program inorder to
    > calculate the total calories, max calories. ???


    Load it in to an editor is all I can suggest since I don't have the code
    you want to modify.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Nov 18, 2005
    #18
  19. Bail

    Zoran Cutura Guest

    Bail <> wrote:
    > void describefoodptr( FOOD *thefoodptr ) {
    > printf( "Food #%d has %f Calories\n", thefoodptr->foodno,
    > thefoodptr->foodcalories );
    >
    > }
    >
    >
    > thefoodptr->foodno???
    >



    If you need to learn C you should simply go and do it. You cannot learn
    from a newsgroup if you don't get the basics at all.
    You'ld better look into your C book (if you don't have one, don't bother
    to write the exame at all). If you have certain questions about the
    -> operator for example somebody in this ng will surely answer it.

    Though you should note, that most are not willing to do your homework.
    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
    Zoran Cutura, Nov 18, 2005
    #19
  20. Bail

    Default User Guest

    Bail wrote:

    > will someone take the time to explain to me how does the program i
    > posted above work? and how do i start modifiy the program inorder to
    > calculate the total calories, max calories. ???


    Not until you learn to post correctly. SEE BELOW.



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Nov 18, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    618
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    542
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    629
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,365
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    460
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page