B
In the title of your question, ask for help for a C++ program. I'll be honest, if you're working with C++ the first thing you need to do is select all your code and delete it, therefore not a line you have written is C+++:The headers <stdlib.h>, <string.h> and <stdio.h> They're from /questions/tagged/c , there are versions adapted to /questions/tagged/c%2b%2b they are <cstdlib>, <cstring> and <cstdio> and they're the ones you should use in case you need them. But it's not your case, you don't need them.In C++ structures (struct) are guys, so they don't need a definition of types (type definition) to be treated as such.In C++ the character strings are handled with the object std::string of the head <string>, they don't manage as character pointers.In C++ file reading is done using an input or output file flow object (std::ifstream and std::ofstream respectively), is not done with FILE*.In C++ it is written in console with the output data flow to console std::cout, not done with the function printf.In C++ memory is requested with the operator new and released with the operator delete, not requested with the function malloc nor is it released with function free.Those are just mistakes of confusing C with C++, but your code has bad practices:The structure personas (plural) stores a single person, his name should be persona (singular).The variables should have a name that helps us understand what your mission is, names like temp, aux, cont, a, b, c, d, e, g, i, n3 and f They don't provide any information on what they're going to use for.Variables should be declared near where they are to be used, this helps to better understand the code, you can declare the indexing variables of your loops. for in the loop itself.Your indentation is a nightmare.If programs in C++ your code might look like:#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <numeric>
struct persona{
std::string nombre;
int codProducto;
int monto;
int dia;
int mes;
int year;
};
template <auto separador>
auto split(const std::basic_string<decltype(separador)> &text)
{
using string = std::basic_string<decltype(separador)>;
std::basic_stringstream<decltype(separador)> reader(text);
std::vector<string> result;
string token;
while (std::getline(reader, token, separador))
result.push_back(token);
return result;
}
int main()
{
std::vector<persona> personas;
if (std::ifstream datos{"prueba_test.data"})
{
std::string linea;
while (std::getline(datos, linea))
{
const auto &registro = split<';'>(linea);
const auto &fecha = split<'-'>(registro[3]);
persona p
{
registro[0],
std::stoi(registro[1]),
std::stoi(registro[2]),
std::stoi(fecha[0]),
std::stoi(fecha[1]),
std::stoi(fecha[2])
};
personas.push_back(p);
}
auto total = std::accumulate(personas.begin(), personas.end(), 0, [](int i, const persona &p) { return i + p.monto; });
std::cout << "Total: " << total << '\n'
<< "Media: " << (total / personas.size());
}
else
std::cout << "No se pudo abrir el fichero.\n";
return 0;
}
You can see the code running in https://tio.run/##jVRba9swGH22f4XIoLFpnNnO5aFKA2OMsYeOPeytDUWRvySijmQkuVsX8teX6WLH7tpCTQjWd853PxatqoSWhG9Ppw@M07IuAC2YUFoC2S/DzmYsjG/7ls0rpJemR6BayL6F13uQjC7D0LBrqlEFUglODiEyj9LF1ZVPhrjYryVgZ2dcIyqKH1IUxkd0xr3g/WPBSA8D1R2egEgcHnEYPgpWoIJooaI4PIQB26DI5RVNTx48DCpZw5rca1B6bExkcIzDwDgEDkeLBRp8IgRnWYanOc7yxPzyNMvv@MCQgsFnSnGe53iKs0mSZhabtNgXADyZTLDxmyZp/gz7ut3i6XSK5/M5zmZJOnmGfmMMz2YznKUpzuZJOm1RHAbH8BiGGvZVSbSZNam1QAoqIklht@DPVcl0RAVX2o97TRSj937oiwJoqZ8qiM5u8RJdaPit7azsMGtll9Ps6PqdIczYz@vtk/3AX89qkAJk5HLjzt1LqlWkYam61C3uitLiAXiT8deOldDsdwu6ZBwiH3jkeaNuQHHsXOzjo46rWu3u14Q@RI4bN0El6Fryc2ozcyc3wrhXVCMuww76JTdKX7aSV5ZwVh97r/r634jtxyg@CF7r0wUaeY7pLXDegd@8k8KFhC0zkYTdo5XFYoiHy8g74P/JG6A70jETw2z9bycr120QNK2hyh5cvuBMSlcjZ2gaEKzzz1bxm1j@EnOlmHhvANlbgAll7cdntaremivfxtH@ua610KRsZU4orfe1/bais@/alGnWPjovdQy8sOd0hG5XkRUGGyE/x3Y4F1WMDq2KGLpE1djfY@jo87tsVNTaXTI/bQ1XaGAPvh7zMrzjQ9uEJdyAufcaQuQZH7t6FPsDURy76yGAUsGLBN/tLYGquhCIrCWTCEq0YXQHUozdxRIGTbGpUfvp9JduSrJVp8SEuaaXlzn5Bw I used a function. split based on https://es.stackoverflow.com/questions/122696/como-leer-numeros-separados-por-comas-en-c/122721#122721https://es.stackoverflow.com/questions/122696/como-leer-numeros-separados-por-comas-en-c/122721#122721 .