The simplest solution to use would be a struct.
http://www.cplusplus.com/doc/tutorial/structures/
(@kunz - I get the impression that you haven't covered classes fully yet??)
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
struct product {
int weight;
double price;
};
// "borrowed" from tutorial on structs
const int productCount = 4;
// better to use a const than a literal as can then change the size of
// the array be altering one const rather than changing assorted loops,
// if conditions and the like.
product myProducts[productCount] = {0};
// the = {0} zeros all array elements
myProducts[0].weight = 4;
myProducts[0].price = 1.25;
myProducts[1].weight = 4;
myProducts[1].price = 1.25;
// etc.
|
But as C++ programmer you should be working towards a nicely encapsulated, class-based solutions, as outlined by minomic:
http://www.cplusplus.com/doc/tutorial/classes/
But I wouldn't necessarily use a "Sandwich" class. Think generalization/specialization: What happens when the cafe starts selling other products? Does a sandwich behave differently than other products? Etc.
And it looks like all the Roll versions costs 50c more than the corresponding plain version, so it might be better to handle rolls as an extra rather than giving each sandwich type two prices (What happens when you start to sell paninis and thins, too? Four prices for each sandwich?)
Aside:
double is the default floating-point type to use these days. I'd only expect to see float used when you're trying to save memory (which means LOTS of value!) and you know the reduced precision won't cause any issues.
Yes, the price of a sandwich could be handled satisfactorily by a float, but it would be better to get into the habit of using the right type from the off!
(You could even use an int for the prices if you stored them in cents/cents/pence/... rather than dollar/euros/pounds/... In some cases it's better to deal with moneyin this way, as it only usually has 2dp.)
Andy
PS When do you use float and when do you use double
http://programmers.stackexchange.com/questions/188721/when-do-you-use-float-and-when-do-you-use-double
n summary, float and long double should be reserved for use by the specialists, with double for "every-day" use. |