Use a map to create a table of foods and calories per portion. for example carrots-45, ice cream-250, and so on. place at least 10 foods in your map. use a random number generator to pick 4 foods per meal. print out the meal and its calorie total.
Use Map to create a table containing food and its corresponding calories, such as carrot-45, ice cream-250, and so on. Map contains at least 10 types of food, 4 types of food are randomly selected for each meal, and the selected food and its calories are printed.
// BookProgramCompiled with vcsp6
# Include <iostream>
# Include <string>
# Include <stdlib. h>
Using namespace STD;
# Include <map>
Typedef STD: Map <string, int> food;
Int main ()
{
Food food;
Int A [5] = {0 };
Int I = 0;
Cout <"there are several kinds of food \ n ";
Cout <"1: Mutton" <Endl;
Cout <"2: Tomato" <Endl;
Cout <"3: potato" <Endl;
Cout <"4: carrot" <Endl;
Cout <"5: Pumpkin" <Endl;
Cout <"6: Pork" <Endl;
Cout <"7: beef" <Endl;
Cout <"8: Onion" <Endl;
Cout <"9: Spinach" <Endl;
Cout <"10: radish" <Endl;
Cout <"11: laver" <Endl;
// Put all the food
// Insert the corresponding food and Caroline
Food. insert (Food: value_type ("mutton", 300 ));
Food. insert (Food: value_type ("tomato", 50 ));
Food. insert (Food: value_type ("potato", 45 ));
Food. insert (Food: value_type ("carrot", 45 ));
Food. insert (Food: value_type ("Pumpkin", 50 ));
Food. insert (Food: value_type ("pork", 300 ));
Food. insert (Food: value_type ("beef", 400 ));
Food. insert (Food: value_type ("Onion", 100 ));
Food. insert (Food: value_type ("Spinach", 100 ));
Food. insert (Food: value_type ("radish", 50 ));
Food. insert (Food: value_type ("laver", 70 ));
Cout <"your food calories: \ nfood \ t \ tcalories \ n ";
While (I <4)
{
Int flag = 1 + rand () % 10; // use a random function
Int J = 0;
For (Food: const_iterator iter = food. Begin (); iter! = Food. End (); ++ ITER, ++ J)
{
If (flag = J & A [J]! = 1)
{
Cout <ITER-> first <"\ t" <ITER-> second <Endl;
I ++;
A [J] = 1;
Break;
}
}
}
Return 0;
}