Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Help on understanding Structs (http://www.velocityreviews.com/forums/t440189-help-on-understanding-structs.html)

Bail 11-17-2005 05:02 AM

Help on understanding Structs
 
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[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 11-17-2005 08:48 AM

Re: Help on understanding Structs
 
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 11-17-2005 09:22 AM

Re: Help on understanding Structs
 
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 11-17-2005 11:43 AM

Re: Help on understanding Structs
 
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 11-17-2005 12:12 PM

Re: Help on understanding Structs
 

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 11-17-2005 03:27 PM

Re: Help on understanding Structs
 
huh? i dont get where to put this code into my program to make it work?


fb 11-17-2005 07:57 PM

Re: Help on understanding Structs
 
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 11-17-2005 08:32 PM

Re: Help on understanding Structs
 
Come on some one help me out here.... dont get this stuffs.


fb 11-17-2005 10:03 PM

Re: Help on understanding Structs
 
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 11-17-2005 10:16 PM

Re: Help on understanding Structs
 
thanks for the info. is there any more ??? help a poor student here



All times are GMT. The time now is 10:19 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.