Confused

I'm am making a code to find votes, amount of votes and percentage.
but whenever I try to find the percentage I get inf. what am I doing wrong?





#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <conio.h>
#include <math.h>

using namespace std;

void getInfo(ifstream& in,string lname[], double votes[], int arraysize);
void CalculateTotal(double votes[], double totalVotes, int arraysize);
void CalculatePercent(double votes[], double totalVotes, double percentage, int arraysize);

int main()
{
ifstream in;
in.open("input.txt");

string lname[5] = { " " };
double votes[5] = { 0.0 };
double percentage = double();
double totalVotes = double();
int arraysize = 5;
//cout << fixed << setprecision(0);
cout << setfill(' ') << left << setw(15) << "Candidates"
<< right << setw(20) << "Votes Recieved" << right
<< setw(25) << "% of Total Votes" << endl;


getInfo(in, lname, votes, arraysize);
CalculateTotal(votes, totalVotes,arraysize);
CalculatePercent(votes, totalVotes, percentage, arraysize);

}
void getInfo(ifstream& in, string lname[], double votes[], int arraysize)
{
for (int i = 0; i < arraysize; i++)
{
in >> lname[i] >> votes[i];
}
}
void CalculateTotal(double votes[], double totalVotes,int arraysize)
{
for (int i = 0; i < arraysize; i++)
{
totalVotes += votes[i];
}
}
void CalculatePercent(double votes[], double totalVotes, double percentage, int arraysize)
{
for (int i = 0; i < arraysize; i++)
{
percentage = votes[i] / totalVotes;
cout << percentage << endl;
}
}
It could be because you don't pass the totalVotes by reference. So when you calculate totalVotes inside the CalculateTotal function, it only lives inside that function. It doesn't change the value of totalVotes in the main function. Then you try and pass it into the CalculatePercent function. Since it has basically not been initialized to anything. It gives you some garbage as an answer. Try passing it in by reference in the calculateTotal function and see if that changes things for you.

void CalculateTotal(double votes[], double& totalVotes, int arraysize);
yeah it worked thanks.
Topic archived. No new replies allowed.