can't see my data stored in a (VECTOR/STRUCT)

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;
;};

typedef vector <election_2020> vector_elec ; // revisado
typedef vector <race_sex> vector_raza; // revisado
typedef vector <age> vector_edad; // revisado

//LECTOR DATOS
void leer_datos(vector_elec& L_elec,vector_raza& L_raza, vector_edad& L_edad ){ // revisado

ifstream datos_elec( "elecciones.txt ");
election_2020 E;

while ( datos_elec >>E.estados >>E.total_poblacion >>E.total_votantes >>E.total_votos>>E.porcentaje_poblacion>>E.porcentaje_votantes>>E.democratas>>E.republicanos )
{
L_elec.push_back(E);
}
ifstream datos_raza( "raza.txt ");
race_sex R;

while( datos_raza >> R.estados >> R.sexo_raza >> R.poblacion_total >> R.votantes_totales >> R.total_registrado >> R.porcentaje_registrado >> R.votos_totales >> R.porcentaje_votantes >> R.porcentaje_poblacion)
{
L_raza.push_back(R);
}

ifstream datos_edad( "edad.txt ");
age A;

while ( datos_edad>> A.estados >> A.poblacion_total >> A.votantes_totales >> A.votos_totales >> A.porcentaje_votos_totales >> A.porcentaje_ciudadano ){
L_edad.push_back(A);
}
cout<<"LECTURA FINALIZADA";
}


int main() {

vector_elec L_elec;
vector_raza L_raza;
vector_edad L_edad;

leer_datos(L_elec,L_raza,L_edad);
cout<<"ok1"<<L_elec[1].estados<<"ok2"<<endl;;
}
what do you get if you do this:

for(auto a: L_elec)
{
cout<<"ok1"<<a.estados<<"ok2"<<endl;
}

you are only printing one item, the second one actually (the first one is [0], not [1]).
Last edited on
Please learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

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.
Last edited on
Whenever you open a disc file you should always test it was opened. If not, don't try to process the file. Especially if the file is opened to read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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.
Last edited on
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.
@George P
Beware always/never rules. Most of the time, if properly structured, you should not have to explicitly test whether a read file was opened.

And don’t return negative numbers from main(). Return zero for success, non-zero (1..127) for failure.
Last edited on
bogus.close(); // it pays to be neat with open files


That contradicts the idea of RAII.
L21. This isn't needed as file streams are closed automatically when the stream goes out of scope - RAII.

Registered users can post here. Sign in or register to post.