Good day folks. I have a simple question anout structs. Say for example i have the following:

struct foodmenu
{
	char letter;
	string food;
	double cost;
	
};

We just started a look at struct and it's really not too clear. So with regards to my example above...how does that replace functions or arrays?

Structs do nothing to replace functions or arrays. Rather, structs make it easier to use functions and arrays when you have multiple pieces of information, possibly of different data types, that relate to a single object.

Using your example, if you did not have structs, you would need three separate arrays to store many food items. Each of these arrays might have to be passed as function arguments, and you have to be careful to use the same index on each array to refer to the same item.

Now, simply make an array of your composite data type, as in:

foodmenu shopping_list[10];

shopping_list[0].letter = 'A';
shopping_list[0].food = "Chicken Noodle Soup";
shopping_list[0].price = 1.45;

//and so on for the other 9 items - a loop would be handy here

With this, a function to display your entire shopping list could have a declaration like:

void show_list( foodmenu list[], int num_items );

Isn't that much easier than passing three arrays, one of char, one of string, one of double?

This is just scratching the surface.

Val

Comments
nice info

So let me just verify something:

struct fastfood
{
	char letter;
	string food;
	double cost;
	
};

ifstream infile("f:\\food.txt");
ofstream print("f:\\mkky.txt");
//********************************** Function Prototypes
void initialize(fastfood mmenu[26]);
void getdata(fastfood mmenu[26]);
void orderprocess(fastfood mmenu[26],double ssubtotal, double &ttotal);
void change(double ttotal);

The above shows the use of a struct...so if i din't want to use a struct, each time in my functions where I have fastfood...i'd have to replace it with:

char letter[];
	string food[];
	double cost[];

??

Also what would i do with the "mmenue[26]", that i currently have in that void initialize function if i didn't use the fastfood struct?Thanks for your input.

void initialize(char letter[26], string food[26], double cost[26]);

Just like your answer said in #4:

>>so if i din't want to use a struct, each time in my functions where I have fastfood...i'd have to replace it with:

char letter[];
string food[];
double cost[];

Thx again. Problem solved and program works like a charm. Thanks to all for their input and pointers/hints. See you around....Thanks again!!

This question has already been answered. Start a new discussion instead.