Scope problems

Oct 1, 2018 at 2:56pm
I'm new to c ++, and I'm still new to vectors. When I compile the code below the following message appears: error: 'congress' was not declared in this scope

unsigned int size = congresso.size ();

What I do not understand is that if I take this part, it will print on the screen that the size is 30. How can I know the size of the scope and solve this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <fstream>
#include "classes.hpp"
#include "eleitores.hpp"
#include <vector>

using namespace std;

void enchevetor(vector<politico>&);
void printavetor(vector<politico>&);
void enchevotos(vector<Eleitores>&);
void printavotos(vector<Eleitores>&);

int main(){

  vector<politico> congresso;
  vector<Eleitores> eleitorado;
  enchevetor(congresso);
  enchevotos(eleitorado);
  printavotos(eleitorado);
  std::cout << congresso.size() << '\n';
  std::cout << eleitorado.size() << '\n';

  //printavetor(congresso);

  return 0;
}

void printavotos(vector<Eleitores>& novoeleitorado){

unsigned int size = novoeleitorado.size();

for(unsigned int i = 0; i<size; i++) {

  std::cout << "Nome do eleitor: " << novoeleitorado[i].getNome()<<endl;
  std::cout << "Votou em: " <<novoeleitorado[i].getPresidente()<< endl ;
  }

}



void enchevetor(vector<politico>& novocongresso){
  ifstream ip("../consulta_cand_2018_BR.csv");

  if(!ip.is_open()) std::cout << "ERROR: File Not Open" << '\n';

  string data, hora, ano, cd_tipo, nm_tipo, turno, cd_eleicao, distrito, data_eleicao, abrangencia, ds_detalhe_situacao, uf, ue, nm_ue, cd_cargo;
  string ds_cargo, sq_candidato, nr_candidato, nm_candidato, nm_urna, nm_social, nr_cpf, nm_email, cd_situacao, ds_situacao,cd_detalhe_situacao, tp_agremiacao, nr_partido, sg_partido, nm_partido, sq_coligacao, nm_coligacao, ds_composicao_coligacao,
   cd_nacionalidade, ds_nacionalidade, sg_uf_nascimento, cd_municipio_nascimento, nm_municipio_nascimento, dt_nascimento, nr_idade_data_posse, nr_titulo_eleitoral,cd_genero, ds_genero, cd_grau_instituicao,
   ds_grau_instrucao, cd_estado_civil, ds_estado_civil, cd_cor_raca, ds_cor_raca, cd_ocupacao, ds_ocupacao, nr_despesa, cd_sit_tot, ds_sit_tot, st_reeleicao, st_declarar_bens, nr_protocolo, nr_processo;

  while(ip.good()){

    getline(ip,data,';');
    getline(ip,hora,';');
    getline(ip,ano,';');
    getline(ip,cd_tipo,';');
    getline(ip,nm_tipo,';');
    getline(ip,turno,';');
    getline(ip,cd_eleicao,';');
    getline(ip,distrito,';');
    getline(ip,data_eleicao,';');
    getline(ip,abrangencia,';');
    getline(ip,uf,';');
    getline(ip,ue,';');
    getline(ip,nm_ue,';');
    getline(ip,cd_cargo,';');
    getline(ip,ds_cargo,';');
    getline(ip,sq_candidato,';');
    getline(ip,nr_candidato,';');
    getline(ip,nm_candidato,';');
    getline(ip,nm_urna,';');
    getline(ip,nm_social,';');
    getline(ip,nr_cpf,';');
    getline(ip,nm_email,';');
    getline(ip,cd_situacao,';');
    getline(ip,ds_situacao,';');
    getline(ip,cd_detalhe_situacao,';');
    getline(ip,ds_detalhe_situacao,';');
    getline(ip,tp_agremiacao,';');
    getline(ip,nr_partido,';');
    getline(ip,sg_partido,';');
    getline(ip,nm_partido,';');
    getline(ip,sq_coligacao,';');
    getline(ip,nm_coligacao,';');
    getline(ip,ds_composicao_coligacao,';');
    getline(ip,cd_nacionalidade,';');
    getline(ip,ds_nacionalidade,';');
    getline(ip,sg_uf_nascimento,';');
    getline(ip,cd_municipio_nascimento,';');
    getline(ip,nm_municipio_nascimento,';');
    getline(ip,dt_nascimento,';');
    getline(ip,nr_idade_data_posse,';');
    getline(ip,nr_titulo_eleitoral,';');
    getline(ip,cd_genero,';');
    getline(ip,ds_genero,';');
    getline(ip,cd_grau_instituicao,';');
    getline(ip,ds_grau_instrucao,';');
    getline(ip,cd_estado_civil,';');
    getline(ip,ds_estado_civil,';');
    getline(ip,cd_cor_raca,';');
    getline(ip,ds_cor_raca,';');
    getline(ip,cd_ocupacao,';');
    getline(ip,ds_ocupacao,';');
    getline(ip,nr_despesa,';');
    getline(ip,cd_sit_tot,';');
    getline(ip,ds_sit_tot,';');
    getline(ip,st_reeleicao,';');
    getline(ip,st_declarar_bens,';');
    getline(ip,nr_protocolo,';');
    getline(ip,nr_processo,'\n');


    politico novopolitico(nm_urna, nr_candidato, nm_partido, ds_cargo, ds_situacao);
    novocongresso.push_back(novopolitico);
  }

  //string teste = novocongresso[2].getNumero();
  //int numb;
  //stringstream ( teste ) >> numb;
  //std::cout << numb;

  ip.close();
}
void printavetor(vector<politico>& novocongresso){

  unsigned int size = novocongresso.size();
  for(unsigned int i = 0; i<size; i++){

    std::cout << "Candidato: " <<novocongresso[i].getCandidato() <<endl;
    std::cout << "Numero: " <<novocongresso[i].getNumero() <<endl;
    std::cout << "Partido: " <<novocongresso[i].getPartido() <<endl;
    std::cout << "Situacao: " << novocongresso[i].getSituacao()<<endl;
    std::cout << "Cargo: "<< novocongresso[i].getCargo()<<endl;



  }

}
void enchevotos(vector<Eleitores>& novoeleitorado){

  string numero_presidente;
  string nome_eleitor;
  int confirma = 0;
  unsigned int size = congresso.size();
  std::cout << size << '\n';

  cout << "Insira o numero de eleitores: " << endl;
  int eleitores=0;
  cin >> eleitores;
  for (int k = 0; k< eleitores; k++){

    std::cout << "Primeiro nome sem acento: ";
    std::cin >> nome_eleitor;
    cout << "Voto para presidende(Numero de dois digitos: xx): ";
    cin >> numero_presidente;
    numero_presidente.push_back('\"');//uma leve malandragem
    numero_presidente.insert(0,1,'\"');

    /*for (unsigned int j = 0; j < size; j++) {

      if (numero_presidente == novocongresso[j].getNumero() && novocongresso[j].getSituacao() == "APTO" && novocongresso[j].getCargo() == "PRESIDENTE" ) {
        std::cout << "Candidato: " <<novocongresso[j].getCandidato() <<endl;
        if (numero_presidente == novocongresso[j].getNumero() && novocongresso[j].getSituacao() == "APTO" && novocongresso[j].getCargo() == "VICE-PRESIDENTE" ){
          std::cout << "Vice:" << novocongresso[j].getCandidato()<<endl;
        }
        std::cout << "Numero: " <<novocongresso[j].getNumero() <<endl;
        std::cout << "Partido: " <<novocongresso[j].getPartido() <<endl;
        std::cout << "Cargo: "<<novocongresso[j].getCargo()<<endl;
        std::cout << "Para confirmar tecle 0, para corrigir tecle 1." << '\n';
        std::cin >> confirma;
      }
    }*/

    Eleitores novoeleitores(nome_eleitor, numero_presidente);
    novoeleitorado.push_back(novoeleitores);


  }

}
Oct 1, 2018 at 3:06pm
the code you refer to, unsigned int size = congresso.size (); s at line 147

congresso cannot be accessed here because it is declared within main() so is out of scope for "enchevotos()"

I think you meant unsigned int size = novoeleitorado.size ();
Oct 1, 2018 at 3:10pm
closed account (E0p9LyTq)
Change line 147 from
unsigned int size = congresso.size(); to
unsigned int size = novoeleitorado.size();

A variable created in main is not visible, it is out of scope, in another function.

Look at the vector's type you are passing. You are not passing std::vector<politico>, you are passing std::vector<Eleitores>.

You are not using the variable you create in your enchevotos(vector<Eleitores>& novoeleitorado) function, you are trying to use variables without creating them first or passing them into your function.
Oct 1, 2018 at 3:27pm
how could I use congresso.size() outside of it's scope? Is it possible?
Oct 1, 2018 at 3:40pm
I'll explain it better, this would be a simulation of voting, in the
1
2
vector<politico> congresso;
void enchevetor (vector <politico> &);

I am putting the information of the candidates and their respective numbers, in the vector
1
2
vector<Eleitores> eleitorado;
 void enchevotos (vector <Eleitores> &);

I'm picking up each vote number. What do I want to do and compare them to see if there is a candidate with that number and keep the vote, is that possible?
Oct 1, 2018 at 6:00pm
If you need to use your congresso object in the enchevotos() function, then just pass it into the function as an argument.

Or, if you only need the size of it, then just pass the size in.
Topic archived. No new replies allowed.