patterncppMinor
Storing data about food
Viewed 0 times
foodstoringdataabout
Problem
I'm trying to make a simple app to help me store data about food to use this database in a future diet application. What can I do to improve the code? What part of code should I make a class etc.?
#include
#include
#include
#include
#include
using namespace std;
struct food
{
int mass;
double kcal;
double prot;
double carb;
double fat;
};
map foodMap_prot;
map foodMap_carb;
map foodMap_fat;
int main()
{
int mass;
double kcal;
double prot;
double carb;
double fat;
string name;
cout > mass;
cout > kcal;
cout > prot;
cout > carb;
cout > fat;
string kategory;
double factor;
kcal /= 100;
prot /=100;
carb /=100;
fat /=100;
food temp1 = {mass, mass*kcal, mass*prot, mass*carb, mass*fat};
map::iterator itr, koniec;
if(prot>carb && prot>fat)
{
factor = prot*100/7;
mass = mass/factor;
temp1 = {mass, mass*kcal, mass*prot, mass*carb, mass*fat};
foodMap_prot[name] = temp1;
itr = foodMap_prot.find(name);
koniec = foodMap_prot.end();
kategory = "Protein";
}
else if(carb>prot&&carb>fat)
{
factor = carb*100/9;
mass = mass/factor;
temp1 = {mass, mass*kcal, mass*prot, mass*carb, mass*fat};
foodMap_carb[name] = temp1;
itr = foodMap_carb.find(name);
koniec = foodMap_carb.end();
kategory = "Carbs";
}
else if(fat>prot&&fat>carb)
{
factor = fat*100/1.5;
mass = mass/factor;
temp1 = {mass, mass*kcal, mass*prot, mass*carb, mass*fat};
foodMap_fat[name] = temp1;
itr = foodMap_fat.find(name);
koniec = foodMap_fat.end();
kategory = "Fat";
}
for(;itr!=koniec; ++itr)
{
cout second.masssecond.kcalsecond.protsecond.carbsecond.fat<<"g";
}
}Solution
Namespace std
Stop doing this:
See every other C++ code review and Why is “using namespace std;” considered bad practice?
Monolithic Functions
Break your code into multiple smaller functions. A good rule of thumb is no more than a dozen lines. If you give your function meaningful names then your code becomes self documenting.
Global Variables
Global variables are a bad idea. It makes testing hard. It makes debugging hard. You don't the result of the function biased on its inputs (it also depends on some other external global state that other functions may mutate while you are not watching.
Pass parameters to functions that should be the only state the worry about.
Objects
This looks like it should be an object:
How about
Looks like all the code in your main is designed to correctly fill up the map. So this should really be part of the FoodMap class. The class should be the only thing defining where things go internally. If you let functions (like main) fill up the map you have no control to validate input.
Stop doing this:
using namespace std;See every other C++ code review and Why is “using namespace std;” considered bad practice?
Monolithic Functions
Break your code into multiple smaller functions. A good rule of thumb is no more than a dozen lines. If you give your function meaningful names then your code becomes self documenting.
Global Variables
Global variables are a bad idea. It makes testing hard. It makes debugging hard. You don't the result of the function biased on its inputs (it also depends on some other external global state that other functions may mutate while you are not watching.
Pass parameters to functions that should be the only state the worry about.
Objects
This looks like it should be an object:
map foodMap_prot;
map foodMap_carb;
map foodMap_fat;How about
class FoodMap
{
typedef std::map FoodMap;
FoodMap protein; // avoid the temptation to shorten names
FoodMap carbs; // unless like this they are hard to spell.
FoodMap fat;
};
// Note on naming conventions.
// User defined types (usually) start with an Uppercase letters.
// Object usually start with a lowercase letter.
// This makes it easy to spot types over objects.Looks like all the code in your main is designed to correctly fill up the map. So this should really be part of the FoodMap class. The class should be the only thing defining where things go internally. If you let functions (like main) fill up the map you have no control to validate input.
Code Snippets
using namespace std;map<string, food> foodMap_prot;
map<string, food> foodMap_carb;
map<string, food> foodMap_fat;class FoodMap
{
typedef std::map<std::string, Food> FoodMap;
FoodMap protein; // avoid the temptation to shorten names
FoodMap carbs; // unless like this they are hard to spell.
FoodMap fat;
};
// Note on naming conventions.
// User defined types (usually) start with an Uppercase letters.
// Object usually start with a lowercase letter.
// This makes it easy to spot types over objects.Context
StackExchange Code Review Q#85571, answer score: 3
Revisions (0)
No revisions yet.