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

using namespace std;

string constestantName(istream &);
void getJudgeData(istream &, string);
void CalcScore(ostream &, string, double, double, double, double, double);
double findLowest(double, double, double, double, double);
double findHeighest(double, double, double, double, double);

int main()
{
	int loopnum, counter, nestcounter;
	string name;
	ifstream infile;
	infile.open("starsearch.txt");
	
	if (infile.fail())
		cout << "file did not open";


	infile >> loopnum;
	cout << loopnum << endl;

	for(counter = 0; counter < loopnum; counter++)
	{
		constestantName(infile);
		name = constestantName(infile);
			for(nestcounter = 0; nestcounter < 5; nestcounter++)
			{
				getJudgeData(infile, name);
			}
	}
	infile.close();

	system("pause");
	return 0;
}

string constestantName(istream &name)
{
	cout << name << endl;
	string contesname;
	name >> contesname;
	return contesname;
	
}

void getJudgeData(istream &score, string name)
{
	double judge1 = -1, judge2 = -1, judge3 = -1, judge4 = -1, judge5 = -1;
	ofstream outfile;
	outfile.open("results.txt");

	if (judge1 == -1)
		score >> judge1;
	else if (judge2 == -1)
		score >> judge2;
	else if (judge3 == - 1)
		score >> judge3;
	else if (judge4 == -1)
		score >> judge4;
	else if (judge5 == -1)
		score >> judge5;
	cout << name << judge1 << judge2 << judge3 << judge4 << judge5;
	CalcScore(outfile, name, judge1, judge2, judge3, judge4, judge5);
}
void CalcScore(ostream &scorefinal, string name, double judge1, double judge2, double judge3, double judge4, double judge5)
{
	double lowest, heighest, avg;
	lowest = findLowest(judge1, judge2, judge3, judge4, judge5);
	heighest = findLowest(judge1, judge2, judge3, judge4, judge5);

	avg = judge1 + judge2 + judge3 + judge4 + judge5 - heighest - lowest / 3;

	scorefinal << name << " \t" << avg << "\n";
}

double findLowest(double judge1, double judge2, double judge3, double judge4, double judge5)
{
	double lowest;

	lowest = judge1;
	if (judge2 < judge1)
	{
		lowest = judge2;
		if (judge3 < judge2)
			lowest = judge3;
		else if (judge4 < judge2)
			lowest = judge4;
		else if (judge5 < judge2)
			lowest = judge5;
	}
	else if (judge3 < judge1)
	{
		lowest = judge3;
		if (judge4 < judge3)
			lowest = judge4;
		else if (judge5 < judge3)
			lowest = judge5;
	}
	else if (judge4 < judge1)
	{
		lowest = judge4;
		if (judge5 < judge4)
			lowest = judge5;
	}
	else if (judge5 < judge1)
		lowest = judge5;
	return lowest;


}
double findheighest(double judge1, double judge2, double judge3, double judge4, double judge5)
{
	double heighest;

	heighest = judge1;
	if (judge2 > judge1)
	{
		heighest = judge2;
		if (judge3 > judge2)
			heighest = judge3;
		else if (judge4 > judge2)
			heighest = judge4;
		else if (judge5 > judge2)
			heighest = judge5;
	}
	else if (judge3 > judge1)
	{
		heighest = judge3;
		if (judge4 > judge3)
			heighest = judge4;
		else if (judge5 > judge3)
			heighest = judge5;
	}
	else if (judge4 > judge1)
	{
		heighest = judge4;
		if (judge5 > judge4)
			heighest = judge5;
	}
	else if (judge5 > judge1)
		heighest = judge5;

	return heighest;
}

The description of the problem is
•Your program will have an input file that will consist of a list of contestants. The first line of the file is an integer that says how many contestants are in the file. Each line is for one contestant and each line in the file would have the following format (the name of the input file starsearch.dat):
Contestant Name(no spaces) score1 score2 score3 score4 score5

•Your program will have a function called string contestantName() that takes the file object as a reference parameter and reads the file for the name of the contestant on the current line and returns the name. This function should be called from main, once for each line of the file.
•The void getJudgeData() will read from the file and not from the user. This function will still use a reference parameter for the score, and it will read the next score from the file. This function will be called five times per contestant and is called from main once for each score.
•The void CalcScore() is used to calculate the score using the same formulation. However, this function will write the contestants name and score to an output file. The function will have 7 parameters instead of 5: output file (pass by reference), contestants' name, and the 5 judges scores.
•The output file will be a list of contestants, where each line has the following format (name the output file results.dat):
Contestant Name(no spaces) Final_Score

In summary, the program is the same as the original version with the additional features: input file, output file, and more than one contestant. This causes the changes in the functions for input and output. The findLowest and findHighest functions (one findes the lowest of the scores the other finds the highest) are still needed as stated in the original description.
My problem is trying to send the file by reference every time i do it sends a mixture of number s and letters instead of the data from the input file.

SAMPLE INPUT FILE

2
Jennifer 10 9 8 9.5 10
Michael 10 9 10 9 10


SAMPLE OUTPUT FILE

Jennifer 9.50
Michael 9.67

Recommended Answers

All 2 Replies

Your code is hard to read. That is the MAIN Problem.
Rewrite the function findhighest and findlowest.
This is an example.

double max(double a, double b){

	return a > b ? a:b;

}
double min(double a, double b){

	return a < b ? a:b;

}

Second instead of using if try to use case and switch.

http://www.cplusplus.com/forum/beginner/5152/

I found the problem. The problem is the average. Please explain me how to calculate the average?

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

using namespace std;

string constestantName(istream &);
void getJudgeData(istream &, string);
void CalcScore(ostream &, string, double, double, double, double, double);
double max(double,double);
double min(double,double);

int main()
{
	int loopnum, counter, nestcounter;
	string name;
	ifstream infile;
	infile.open("starsearch.txt");
	
	if (infile.fail())
		cout << "file did not open";

	infile >> loopnum;
	cout << loopnum << endl;

	for(counter = 0; counter < loopnum; counter++)
	{
		constestantName(infile);
		name = constestantName(infile);
			for(nestcounter = 0; nestcounter < 5; nestcounter++)
			{
				getJudgeData(infile, name);
			}
	}
	infile.close();

	system("pause");
	return 0;
}

string constestantName(istream &name)
{
	cout << name << endl;
	string contesname;
	name >> contesname;
	return contesname;
	
}

void getJudgeData(istream &score, string name)
{
	double judge1 = -1, judge2 = -1, judge3 = -1, judge4 = -1, judge5 = -1;
	ofstream outfile;
	outfile.open("results.txt");

	if (judge1 == -1)
		score >> judge1;
	else if (judge2 == -1)
		score >> judge2;
	else if (judge3 == - 1)
		score >> judge3;
	else if (judge4 == -1)
		score >> judge4;
	else if (judge5 == -1)
		score >> judge5;
	cout << name << judge1 << judge2 << judge3 << judge4 << judge5;
	CalcScore(outfile, name, judge1, judge2, judge3, judge4, judge5);
}
void CalcScore(ostream &scorefinal, string name, double judge1, double judge2, double judge3, double judge4, double judge5)
{
	double lowest, heighest, avg;
	lowest = min(judge1,min(judge2,min(judge3,min(judge4,judge5))));
	heighest = max(judge1,max(judge2,max(judge3,min(judge4,judge5))));

	avg = judge1 + judge2 + judge3 + judge4 + judge5 - heighest - lowest / 3;//This line makes no sense to me.
	
	scorefinal << name << " \t" << avg << "\n";
}

double max(double a, double b){

	return a > b ? a:b;

}
double min(double a, double b){

	return a < b ? a:b;

}
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.