If the input is for example "banana", I want to print the kcal of banana. I tried something like this (and failed):
string input;
cin >> input;
cout << input.Kcal << endl;
I know that I can do it with if-statements like:
string input;
cin >> input;
if(input == "banana")
{
cout << banana.Kcal << endl;
}
But there I must write very much code when I have more then 1000 foods...
There is my declaration and definition of my banana object. Every object has kcal.
food banana;
banana.Kcal = 89;
My class, the Food.h code:
#pragma once
class CFood
{
public:
CFood();
~CFood();
float Kcal;
}
The food.cpp code:
CFood::CFood()
{
Kcal = 0;
}
CFood::~CFood()
{
}
std::unordered_map<std::string, CFood>would seem a reasonable fit for your needs.