Strange outputs using vectors!

Does anyone know why this code is yielding the strange numbers in the Program Output that I never input?? Let me apologize beforehand for the poor formatting of my post; I tried using the formatting tools on here (which I have successfully used several times before) but to no avail!!


#include <iostream>
#include <vector>
using namespace std;

const int noOfCandidates = 5;


struct candidate
{
string name;
int votes;
};



void getNameAndVote (vector<candidate>& vec, int length);

int main ()
{
int totalVotes;
double percentVotes[noOfCandidates];
vector<candidate>::iterator iter;


for(int i = 0; i < 10; i++)
{
vector<candidate> election(noOfCandidates);

getNameAndVote(election, noOfCandidates);

cout<<endl<<endl;

for (iter = election.begin(); iter != election.end(); ++iter)
//cout<<(*iter).name<<" "<<(*iter).votes<<endl;
cout<<iter->name<<" "<<iter->votes<<endl;

cout<<endl<<endl<<endl;
}

// wait until user is ready before terminating program
system("PAUSE");
return 0;
}



void getNameAndVote (vector<candidate>& vec, int length)
{
int i;
candidate cand;

cout<<"In fn getNameAndVote, size of f.p. vec: "<<vec.size()<<endl<<endl;

for (i = 0; i < length; i++)
{
cout<<"Enter a candidate's name: ";
cin>>cand.name;

cout<<"Enter votes received by candidate: ";
cin>>cand.votes;

vec.push_back(cand);

cout<<endl;
}
}





PROGRAM OUTPUT
***************

In fn getNameAndVote, size of f.p. vec: 5

Enter a candidate's name: Andy
Enter votes received by candidate: 40

Enter a candidate's name: Bella
Enter votes received by candidate: 10

Enter a candidate's name: Carla
Enter votes received by candidate: 30

Enter a candidate's name: Dele
Enter votes received by candidate: 50

Enter a candidate's name: Emma
Enter votes received by candidate: 20



1064832536
1064832536
1064832536
1064832536
1064832536
Andy 40
Bella 10
Carla 30
Dele 50
Emma 20



In fn getNameAndVote, size of f.p. vec: 5

Enter a candidate's name:


Can you put it in code tags please? Kind of unreadable (to me) in it's current format sorry.

edit:
it looks like it has something to do with the fact you are iterating from 0 to 9 in your first loop, but then 0 to 4 in your other loop.
Last edited on
Line 27: you are creating a vector containing 5 default-initialized objects.
Line 29: you are passing that vector by reference
Line 62: You are adding new element after 5(or more) already existing objects.
Wow! Surprisingly, the formatting tools are now working! Anyway here is the formatted code and output. I don't think the problem is what you suggested about the difference in loop sizes: each iteration of the loop in function main() calls function getNameAndVote where the vector is populated with data before returning.

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
#include <iostream>
#include <vector>
using namespace std;  

const int noOfCandidates = 5;


struct candidate
{
	string name;              
	int votes;
};



void getNameAndVote (vector<candidate>& vec, int length);  

int main ()
{
  int totalVotes;
  double percentVotes[noOfCandidates];
  vector<candidate>::iterator iter; 
  
   
  for(int i = 0; i < 10; i++)          
  {  
      vector<candidate> election(noOfCandidates);    
	  
      getNameAndVote(election, noOfCandidates);
	  
      cout<<endl<<endl;
	  
	  for (iter = election.begin(); iter != election.end(); ++iter)
	      //cout<<(*iter).name<<"     "<<(*iter).votes<<endl;
	      cout<<iter->name<<"     "<<iter->votes<<endl;
       
      cout<<endl<<endl<<endl;
  }  

  // wait until user is ready before terminating program
  system("PAUSE");
  return 0; 
}



void getNameAndVote (vector<candidate>& vec, int length) 
{
  int i;
  candidate cand;
  
  cout<<"In fn getNameAndVote, size of f.p. vec: "<<vec.size()<<endl<<endl;
  
  for (i = 0; i < length; i++)
  {
	cout<<"Enter a candidate's name: ";
    cin>>cand.name;
    
    cout<<"Enter votes received by candidate: ";
    cin>>cand.votes; 
    
    vec.push_back(cand);
    
    cout<<endl;
  }
}




Here is the program output after only one iteration of the FOR loop in main():

In fn getNameAndVote, size of f.p. vec: 5

Enter a candidate's name: Andy
Enter votes received by candidate: 40

Enter a candidate's name: Bella
Enter votes received by candidate: 10

Enter a candidate's name: Carla
Enter votes received by candidate: 30

Enter a candidate's name: Dele
Enter votes received by candidate: 50

Enter a candidate's name: Emma
Enter votes received by candidate: 20



     1064832536
     1064832536
     1064832536
     1064832536
     1064832536
Andy     40
Bella     10
Carla     30
Dele     50
Emma     20



In fn getNameAndVote, size of f.p. vec: 5

Enter a candidate's name:
Read my previous message with explanation on this behavior.
Like MiiNiPaa said. On line 27 you create a vector with 5 [empty] elements. These are shown before the entered.

Change line 27 to vector<candidate> election(noOfCandidates);
Thank you MiiNiPaa; I am sorry I did not see your first post before my last post! Your explanation is spot-on!

Thanks also coder777; I tried your suggestion and got the result I was expecting.
Topic archived. No new replies allowed.