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
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.
#include <iostream>
#include <vector>
usingnamespace std;
constint 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: