I have a text file with names, wins, losse. I have to take those in a write a function to find the average, and a function to sort the names in alphabetical order. I finally got everything into arrays but I cant do anything else. I don't think I'm understanding how to call an array to divide.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

const int MAX_SCORES = 50;
ifstream infile;

void report(int numItems, string name[], int wins[], int losses[]);
int readData(string name[], int wins[], int losses[]);
void findAverage(int wins[], int losses[]);

int main()
{

string name[MAX_SCORES];
int wins[MAX_SCORES];
int losses[MAX_SCORES];
int numItems;
double average;

numItems = readData(name, wins, losses);
average = findAverage(wins, losses);

report(numItems,name,wins,losses);
findAverage(wins,losses);

}

int readData(string name[], int wins[], int losses[])
{
	infile.open("players.txt");
	
	int count=0;
	while(infile.peek() !=EOF)
	{
		infile >> name[count] >> wins[count] >> losses[count];
		count++;
		
		infile.ignore(1);
	}
	infile.close();
	return count;
}

void report(int numItems, string name[], int wins[], int losses[])
{
	for (int i=0; i<numItems; i++)
	cout << name[i] << " " << wins[i] << " " << losses[i] << endl;
	
}

void findAverage(int numItems, int wins[], int losses[])
{
	double average;
	for(int i= 0; i <= numItems; i++)
	      		average= (wins[numItems]*100)/losses[numItems];
	 
		cout<< "The average value is: "<<average<<endl;
	
}
/*void bubbleSort (int numItems, string name[], int wins[], int losses[])
{
	string temp;
	int iteration;
	int index;
	
	for (iteration=1;iteration<numItems;iteration++)
	{
		for (index=0; index=numItems-iteration;index++)
		if (name[index]>name[index+1]);
	}
	temp=name[index];
	name[index]=name[index+1];
	name[index+1]=temp;
	cout << temp << endl;
}*/

>>average= (wins[numItems]*100)/losses[numItems];

That will crash when losses[numItems] is 0 because division by 0 is undefined. Test losses[numItems] for 0 before performing that division.

I think the whole algorithm in that function is incorrect. To get the average you have to sum them all up and then perform the division

int total_wins = 0;
int total_losses = 0;
for(int i = 0; i < numItems; i++)
{
   total_wins += wins[i];
   total_losses += losses[i];
}
if(total_losses == 0)
   total_losses = 1; // prevent division by 0 error
float average = (float)total_wins/(float)total_losses;

the file looks like this
student name 5 3
student name 2 9
student name 9 2

there are 15 names and scores. the first is wins the second is losses. I have to find win percentage so the math would be wins/(wins+losses). and multiple by 100 to force the double.

With the original code I get a floating point exception.

well, then just change the formula in the code I posted

int total_wins = 0;
int total_losses = 0;
for(int i = 0; i < numItems; i++)
{
   total_wins += wins[i];
   total_losses += losses[i];
}
float average = (float)total_wins/(float)(total_wins + total_losses);
float average = (float)total_wins/(float)(total_wins + total_losses);

so you redeclare average in the statement, and then redeclare total_wins ans total_losses?
Sorry I'm not understanding.

and also the

total_wins += wins[i];
   total_losses += losses[i];

is adding all the wins and losses and getting an average for everything correct?
I need individual averages.

If you want individual averages then just correct the calculation in line 55 of your original post.

So this is what I did to get it to work. But I need two decimal points and I don't know where they went.

void findAverage(int numItems, int wins[], int losses[], double average[])
{
	cout << fixed << setprecision(2) << endl;
	for(int i= 0; i < numItems; i++)
	{
		
		average[i]= (wins[i]*100)/(wins[i]+losses[i]);
		cout << average[i] << endl;
	}

you have to typecast either the numerator or denominator to float or double average[i]= (double)((wins[i]*100))/(wins[i]+losses[i]);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.