Velocity Reviews > Help on understanding Structs

# Help on understanding Structs

Bail
Guest
Posts: n/a

 11-17-2005
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(" 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[i] = 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[i].name);
printf(" calories: %d\n", foods[i].calories);
printf(" group # : %d\n", foods[i].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

vietor
Guest
Posts: n/a

 11-17-2005
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[i].calories;
if(foods[i].calories>maxcalories)
{
maxcalories=foods[i].calories;
strcpy(maxcaloriesname,foods[i].name);
}

} // end for

//.....

Suman
Guest
Posts: n/a

 11-17-2005
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.

pete
Guest
Posts: n/a

 11-17-2005
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

Suman
Guest
Posts: n/a

 11-17-2005

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.

Bail
Guest
Posts: n/a

 11-17-2005
huh? i dont get where to put this code into my program to make it work?

fb
Guest
Posts: n/a

 11-17-2005
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?

Bail
Guest
Posts: n/a

 11-17-2005
Come on some one help me out here.... dont get this stuffs.

fb
Guest
Posts: n/a

 11-17-2005
Bail wrote:

> Come on some one help me out here.... dont get this stuffs.
>

Ok...try here...it's not very good, but it is simple:

Structures:
http://members.shaw.ca/ipatters/BeginC_10.html

Other C stuff:
http://members.shaw.ca/ipatters/

Bail
Guest
Posts: n/a

 11-17-2005
thanks for the info. is there any more ??? help a poor student here