my code is bellow. I'm trying to call the data stored in the vector (cout command in the last line) and when I run this code it just prints "LECTURA FINALIZADAok1".I would appreciate any help on how to call the data, thanks.
# include <iostream>
# include <fstream>
# include <vector>
using namespace std;
//TUPLA REPUBLICANOS/DEMOCRATAS
struct election_2020 {// revisado
int total_votos, total_votantes, republicanos , democratas, total_poblacion;
string estados ;
float porcentaje_poblacion, porcentaje_votantes;
;};
//TUPLA RAZA
struct race_sex {
string estados;
int sexo_raza, poblacion_total, votantes_totales, total_registrado;
int votos_totales;
float porcentaje_registrado, porcentaje_votantes, porcentaje_poblacion;
;};
//TUPLA EDAD
struct age {
string estados;
int edad, poblacion_total, votantes_totales,votos_totales;
float porcentaje_ciudadano, porcentaje_votos_totales;
;};
Some formatting & indentation would not hurt either
Your 3 vectors defined in main are probably not being filled in the function so they are likely still empty after the function ends, there are no tests to make sure the data files are actually opened for reading.
The data files fail to open and the code still goes ahead trying to read data that doesn't exist.
#include <iostream>
#include <fstream>
int main()
{
// try to open a file that doesn't exist
std::ifstream bogus("no_data.txt");
// check for file open status
if (bogus)
{
std::cout << "File was opened successfully!\n";
}
else
{
std::cout << "File was NOT opened successfully!\n";
return -1; // exit with "error", no need to close an unopened file
}
bogus.close(); // it pays to be neat with open files
}
File was NOT opened successfully!
This is merely a very crude example, how you handle file I/O problems is up to you.
You declare the initial size of a std::vector in its constructor, so one way you can accomplish this is:
struct element
{
char ulica[10];
std::vector<int> dane;
int wolne;
int w;
element *lewy, *prawy, *ojciec;
element() : dane(3) {}
};
If you don't include the constructor, the initial size of the vector will be 0. In any event, to add an element to the back, just use tmp2->dane.push_back(number); This will add the value in number to the back of the vector tmp2->dane which may result in a change in the amount of allocated memory for the vector instance.