C
I have to make default already there are values for the structure [...] and I have default products in a text file that adds them to the structure and there they stay even if I close the program.I'm sorry to have to give you this news, but things don't work that way.C++ objects exist in three levels:Declaration: They are said to exist, but there are no details about the object.
As an analogy, imagine they say to you."There's a new Volkswagüen car model1"you know the model exists, but you don't know anything about it.Definition: The details of the object are given.
As an analogy, imagine that they give you the plans of the new Volkswagüen model.Trial: You create objects.
As an analogy, imagine manufacturing the new car model.When Instances an object, the object occupies a space in memory and that memory it occupies has not obtained any value, neither archive nor other origin of https://es.wikipedia.org/wiki/Persistencia_(inform%C3%A1tica) . There is no file-to-memory load automa.If you want to ensure the persistence of your data, it must be you who persists. The strategy you use to do so is your thing, I would create some functions leer and escribir:std::list<tienda> leer(const std::string &archivo)
{
std::list<tienda> resultado;
if (std::ifstream datos{archivo})
{
while (datos)
{
tienda t;
datos >> t.id;
datos >> t.ids;
datos >> t.producto;
datos >> t.precio;
resultado.push_back(t);
}
}
else
std::cout << "No se pudo abrir " << archivo << '\n';
return resultado;
}
void escribir(const std::list<tienda> &tiendas, const std::string &archivo)
{
if (std::ofstream datos{archivo})
{
for (const auto &t : tiendas)
{
datos << t.id << '\n';
datos << t.ids << '\n';
datos << t.producto << '\n';
datos << t.precio << '\n';
}
}
else
std::cout << "No se pudo abrir " << archivo << '\n';
}
On the other hand, you can create information that will be stored in the executable file and will be available when booting the program, so we make some modifications to your structure tintoreria:struct tintoreria{
std::string id;
std::string ids;
std::string descripción;
float precio;
};
Make the text elements of the structure text strings (std::string), then at the entry point you can create a list of tintorería:#include <list>
#include <string>
struct tintoreria{
std::string id;
std::string ids;
std::string descripción;
float precio;
};
int main()
{
std::list<tintoreria> tintorerias
{
// id ids descripción precio
{ "1111", "01", "blusa", 100.f },
{ "1111", "02", "camisa caballero", 100.f },
{ "1111", "03", "traje", 100.f },
{ "1112", "01", "blusa", 100.f },
{ "1112", "02", "camisa caballero", 100.f },
{ "1112", "03", "traje", 100.f },
{ "1112", "04", "pantalon de mezclilla", 100.f },
{ "1112", "05", "sabanas", 100.f },
{ "1113", "01", "zapato formal hombre", 100.f },
{ "1113", "02", "sandalias", 100.f },
{ "1113", "03", "zapato infante", 100.f },
{ "1113", "04", "Moicasines", 100.f },
};
// …
// Hacer cosas
// …
return 0;
}
1Car brand invented, any resemblance to reality is casual.