0

i'm a beginner and i'm trying to get input for "item" in the calculate() function and see if it matches with one from the vector "lista" (get_value() function), i have tried a lot but even if those 2 strings look the same it always turns out that they are not equal. I have also tried compare() and i get -1 value with "item.compare(lista[0].name)" eventhough i am sure that those two strings should be equal :(

Here is the code

class insumos
{
public:
    std::string name;
    double value;
    insumos(std::string x, double y) : name(x), value(y) {};
    

};
// vector para almacenar la lista de insumos y precios por gramo
std::vector<insumos> lista;

void push_insumos()
{
    lista.push_back(insumos{ "aceite de almendras", 0.19 });
    lista.push_back(insumos{ "aceite de argan", 0.44 });
    lista.push_back(insumos{ "aceite de jojoba", 0.36 });
    lista.push_back(insumos{ "aceite de coco", 0.056 });
    lista.push_back(insumos{ "karite", 0.055 });
    lista.push_back(insumos{ "aceite de palta", 0.19 });
    lista.push_back(insumos{ "aceite de germen de trigo", 0.26 });
    lista.push_back(insumos{ "aceite de girasol", 0.16 });
    lista.push_back(insumos{ "aceite de oliva", 0.027 });
    lista.push_back(insumos{ "agua destilada", 0.007 });
    lista.push_back(insumos{ "soda caustica",0.023 });
    lista.push_back(insumos{ "esencial de tomillo", 1.06 });
    lista.push_back(insumos{ "esencial de limon", 0.5 });
    lista.push_back(insumos{ "esencial de naranja", 0.39 });
    lista.push_back(insumos{ "esencial de basil", 1.05 });
    lista.push_back(insumos{ "esencial de gene", 1.5 });
    lista.push_back(insumos{ "esencial de geranio", 1.08 });
    lista.push_back(insumos{ "esencial de incienso", 2.6 });
    lista.push_back(insumos{ "esencial de citro", 0.87 });
    lista.push_back(insumos{ "esencial de romero", 0.76 });
    lista.push_back(insumos{ "esencial de ylang ylang", 1.66 });
    lista.push_back(insumos{ "esencial de cart", 1.16 });
    lista.push_back(insumos{ "esencial de lavanda", 0.98 });
    lista.push_back(insumos{ "vitamina e", 0.83 });
    lista.push_back(insumos{ "madder root", 0.14 });
    lista.push_back(insumos{ "hierbas", 0.005 });

}
//

double get_value(std::string s, double x)
{
    for (int i = 0; i < lista.size(); ++i)
    {
        if (s == lista[i].name) return lista[i].value * x;
    }
    throw std::runtime_error("error");
}


double calculate()
{   
    try
    {
        double resultado = 0;
        double gramos;
        std::string item;
        std::cout << "Hola! cuantos insumos en total utilizaste para esta receta?\n";
        int cant_de_veces;
        std::cin >> cant_de_veces;
        std::cout << "Porfavor ingresa la siguiente informacion para calcular el precio\n";
        for (int i = 0; i < cant_de_veces; ++i)
        {
            std::cout << " Insumo: "; getline(std::cin, item,'.');
            std::cout << " gramos: "; std::cin >> gramos;
            resultado += get_value(item, gramos);
                              
        }
        return resultado;
        
    }
    
    
    catch (std::runtime_error& e)
    {
        std::cerr << e.what();
    }
}
  • The shown code uses `>>` and `std::getline` in a way that's almost certainly wrong, and if you show the values that you think are getting read in, you will see the problem. See the linked question for more information. – Sam Varshavchik Sep 26 '20 at 22:28
  • i've seen the output from std::getline and it shows e.g "aceite de almendras", but when i try the statement "item == lista[0].name" they are not equal – Alberto Alarcon Sep 26 '20 at 23:35

0 Answers0