Help on understanding Structs

B

Bail

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
 
V

vietor

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


//.....
 
S

Suman

Bail said:
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.
 
P

pete

Suman said:
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.
 
S

Suman

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.
 
F

fb

pete said:
Suman wrote:




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?
 
B

Bail

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. ???
 
W

Walter Roberson

: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."
 
W

Walter Roberson

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 );
}
 
B

Bail

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

}


thefoodptr->foodno???
 
I

Ian Malone

Bail wrote:

You cut outand 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.
 
F

Flash Gordon

Bail said:
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.
 
Z

Zoran Cutura

Bail said:
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.
 
D

Default User

Bail said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top