Skip to main content
deleted 4 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

how to use classes to make code more efficient? C++ Storing data about food

ok this is my first post. ImI'm trying to make a simple app to help me store data about food, to use this database in a future diet application. whatWhat can iI do to improve the code? whatWhat part of code should iI make a class etc.?

how to use classes to make code more efficient? C++

ok this is my first post. Im trying to make a simple app to help me store data about food, to use this database in future diet application. what can i do to improve the code? what part of code should i make a class etc.?

Storing data about food

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.?

edited title
Link

what can I do how to use classes to make my code more efficient? C++

Source Link

what can I do to make my code more efficient? C++

ok this is my first post. Im trying to make a simple app to help me store data about food, to use this database in future diet application. what can i do to improve the code? what part of code should i make a class etc.?

    #include <iostream>
    #include <vector>
    #include <string>
    #include <map>
    #include <iterator>


    using namespace std;

    struct food
    {
        int mass;
        double kcal;
        double prot;
        double carb;
        double fat;
    };

    map<string, food> foodMap_prot;
    map<string, food> foodMap_carb;
    map<string, food> foodMap_fat;
    int main()
    {

        int mass;
        double kcal;
        double prot;
        double carb;
        double fat;
        string name;
        cout << "Enter a name for a product: ";
        getline(cin,name,'\n');
        cout << "\nmass of [" << name << "]: ";
        cin >> mass;
        cout << "\ncalories of [" << name << "]: ";
        cin >> kcal;
        cout << "\nprotein in [" << name << "]: ";
        cin >> prot;
        cout << "\ncarbs in [" << name << "]: ";
        cin >> carb;
        cout << "\nfat in [" << name << "]: ";
        cin >> 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<string, food>::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 <<"\n\nyou added a new product \t[" << name <<"]";
    cout <<"\n\ncategory: \t\t\t" << kategory;
    cout <<"\n\nmass of one block of [" << name << "]: \t" << itr->second.mass<<"g";
    cout <<"\n\ncalories in [" << name << "]: \t" << itr->second.kcal<<" kcal";
    cout << "\n\nprotein [" << name << "]: \t"<< itr->second.prot<<"g";
    cout <<"\n\ncarbs [" << name << "]: \t" << itr->second.carb<<"g";
    cout << "\n\nfat [" << name << "]: \t" << itr->second.fat<<"g";
}

    }