473,910 Members | 6,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*sizeo f(char));

printf("\nPleas e 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.calor ies));

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

Nov 17 '05 #1
26 2217
like this:

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

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

getFood( foods, i );
// .......
total_calories+ =foods[i].calories;
if(foods[i].calories>maxca lories)
{
maxcalories=foo ds[i].calories;
strcpy(maxcalor iesname,foods[i].name);
}

} // end for
//.....

Nov 17 '05 #2
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.

Nov 17 '05 #3
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
Nov 17 '05 #4

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.

Nov 17 '05 #5
huh? i dont get where to put this code into my program to make it work?

Nov 17 '05 #6
fb
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?
Nov 17 '05 #7
Come on some one help me out here.... dont get this stuffs.

Nov 17 '05 #8
fb
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/

Nov 17 '05 #9
thanks for the info. is there any more ??? help a poor student here

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4903
by: cpptutor2000 | last post by:
I am using an STL list to store data packets in a network simulator. The data packets are really C structs, which have other C structs inside them. These structs contain unsigned char arrays of various sizes, and some unsigned int values. There are no pointers values stored in these structs. I have a function, from inside of which I have a loop as follows: Please note that dataPacketStore is the name of the struct being stored. ...
3
1719
by: SteveM | last post by:
This is probably an easy answer but since I am fairly new to C++ for some reason I can't see it :-( Any help would be appreciated :-) I have a class: class AClass { public:
8
2997
by: Johnny | last post by:
I need to determine whether a text box contains a value that does not convert to a decimal. If the value does not convert to a decimal, I want to throw a MessageBox to have the user correct the value in the text box. I have the following code but when the user enters a decimal value the Regex.IsMatch catches it (ex. 250.50 should be allowed, but 250.50.0 should not). My code is as follows: if( ! Regex.IsMatch( tboxQtyCounted.Text,...
39
2626
by: gtippery | last post by:
Newbie-ish questions - I've been away from C for a _long_ time. It seems to me that there ought to be easier (or at least shorter) ways to do what this does. It does compile & run for me (with PowerC, a 16-bit DOS compiler); if there are nonstandard or "accidentally-works" aspects, please let me know. {This is the sort of situation where if I knew *what* to Google for, I wouldn't *need* to... <grin>}
10
2081
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required parameter. What would I need to do in order to convert a struct that looks like this: typedef struct { char rsvd0; char iadl1;
23
2264
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have an SDK that I have written for C/C++ and would like to port it to C# if at all possible. Basically, I've got a structure that I need to pass to the C-style DLL (i.e. no mangled names - everything is __stdcall). This structure needs
9
1532
by: ^MisterJingo^ | last post by:
I'm new to C# (literally 2 days) and need a bit of help understanding an error. struct Blah { int x; int y; public int X {
24
2910
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as are structs, where classes are on the heap... when value types go out of scope the memory is re- allocated, object remain in memory waiting to be cleaned up by the garbage collector, etc, but he responded 'so why not just put say a class on the...
4
1784
by: engggirl3000 | last post by:
I was told to write a function that will read data, in a user defined text file, into a vector of structs. I am not sure where to start with this. Could some one tell me what is a vector of structs, I know vectors and I know structs, but I dont know whats a vector of structs. As well, is there a specific algoritm in c++ to use when reading from a user defined text file, or does cin work? If someone can give me a few pointers to help get...
0
10037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10921
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11055
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10541
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8099
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7250
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5939
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4337
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.