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

using namespace std;

string constestantName(istream &); //Calls the the input file from main() by refrencea and returns the name of the contestant
void getJudgeData(istream &, string, int); //gets the scores from starsearch.dat and stores them in five veriables
void CalcScore(ostream &, string, double, double, double, double, double); //calculates the scores and puts them in results.dat
double findLowest(double, double, double, double, double); //finds and return the lowest of the scores
double findHeighest(double, double, double, double, double);// finds nad returns the heighest of the scores

int main()
{
	int loopnum, counter, nestcounter; //loop num is to store the ammount of contestants and counter and nestcounter are counters for loops
	string name; //stores the return value of constestantName()
	ifstream infile; //input file
	infile.open("starsearch.dat"); //open starsearch.dat
	
	if (infile.fail()) // test to see if file is open
		cout << "file did not open" << endl;


	infile >> loopnum; //gets the ammount of contestant
	
	//iterates to the ammount of contestants
	for(counter = 0; counter < loopnum; counter++) 
	{
		name = constestantName(infile); //calls constestantName() and stores the return value in name 
		//nested for loop for calculating the contestants score	
		for(nestcounter = 0; nestcounter < 5; nestcounter++) 
			{
				//calles getJudgeData()
				getJudgeData(infile, name, nestcounter);
			
			}
	}
	
	infile.close(); //closes starsearch.dat
	system("pause");
	return 0;
}

//This function retrieves teh contestants name from starsearch.dat and returns it to main
string constestantName(istream & input) //calles starsearch.dat by refrence
{
	string contesname; //will store the name of the contestant
	input >> contesname; //retrieves the name from starsearch.dat and stores it in contesname
	return contesname; //returns contesname to main
	
}

//This function is sent the starsearch.dat by reference per score number, it is also sent the contestants name and the value of nestcounter
void getJudgeData(istream &score, string name, int counter)
{
	double judge1, judge2, judge3, judge4, judge5; //veriables to store the scores
	ofstream outfile; //output file
	outfile.open("results.dat"); //opens results.dat

	if (counter == 0) //if the counter is == 0 store the score in judge1
		score >> judge1;
	else if (counter == 1) //if the counter is == 1 store the score in judge2
		score >> judge2;
	else if (counter == 2) //if the counter is == 2 store the score in judge3
		score >> judge3;
	else if (counter == 3) //if the counter is == 3 store the score in judge4
		score >> judge4;
	else if (counter == 4) //if the counter is == 4 store the score in judge5
		score >> judge5;

	//if the counter is == 4 send results.dat, name, judge1, judge2, judge3, judge4, and judge5 to CalcScore()
	//if not go back to main
	if (counter == 4) 
		CalcScore(outfile, name, judge1, judge2, judge3, judge4, judge5);
	
}

//This function takes results.dat by refernce, name, judge1, judge2, judge3, judge4, and judge5 and uses it to calcualte the average score and outputs it ot CalcScore()
void CalcScore(ostream &scorefinal, string name, double judge1, double judge2, double judge3, double judge4, double judge5)
{
	double lowest, heighest, avg; //lowest and heightest will store the values of findLowest and findheighest. avg will store the score average
	lowest = findLowest(judge1, judge2, judge3, judge4, judge5);//finds the lowest number of the five scores
	heighest = findHeighest(judge1, judge2, judge3, judge4, judge5);//fines the heighest number of the five scores

	avg = (judge1 + judge2 + judge3 + judge4 + judge5 - heighest - lowest) / 3; //drops the lowest and heighest scores and gets the average 

	scorefinal << name << " \t" << avg << "\n"; //outputs the results to results.dat
}

//This fucntion finds the lowest of the five scores
double findLowest(double judge1, double judge2, double judge3, double judge4, double judge5)
{
	double lowest; // will hold the return value

	lowest = judge1; //assigns judge1 to lowest
	if (judge2 < judge1) //if judge2 is less than judge1 it assign judge2 to lowest
	{
		lowest = judge2;
		if (judge3 < judge2)//if judge3 is less than judge2 it assign judge3 to lowest
			lowest = judge3;
		else if (judge4 < judge2)//if judge4 is less than judge2 it assign judge4 to lowest
			lowest = judge4;
		else if (judge5 < judge2)//if judge5 is less than judge2 it assign judge5 to lowest
			lowest = judge5;
	}
	else if (judge3 < judge1) // else if judge3 is less than judge1 it assign judge3 to lowest
	{
		lowest = judge3;
		if (judge4 < judge3)//if judge4 is less than judge3 it assign judge4 to lowest
			lowest = judge4;
		else if (judge5 < judge3)//if judge5 is less than judge3 it assign judge5 to lowest
			lowest = judge5;
	}
	else if (judge4 < judge1) // else if judge4 is less than judge1 it assign judge4 to lowest
	{
		lowest = judge4;
		if (judge5 < judge4)//if judge5 is less than judge4 it assign judge to lowest
			lowest = judge5;
	}
	else if (judge5 < judge1)// else if judge5 is less than judge1 it assign judge5 to lowest
		lowest = judge5;
	
	return lowest; //returns lowest
}
double findHeighest(double judge1, double judge2, double judge3, double judge4, double judge5)
{
	double heighest; // will hold the return value

	heighest = judge1; //assigns judge1 to heighest
	if (judge2 > judge1) //if judge2 is greater than judge1 it assign judge2 to heighest
	{
		heighest = judge2;
		if (judge3 > judge2)//if judge3 is greater than judge2 it assign judge3 to heighest
			heighest = judge3;
		else if (judge4 > judge2)//if judge4 is greater than judge2 it assign judge4 to heighest
			heighest = judge4;
		else if (judge5 > judge2)//if judge5 is greater than judge2 it assign judge5 to heighest
			heighest = judge5;
	}
	else if (judge3 > judge1) // else if judge3 is greater than judge1 it assign judge3 to heighest
	{
		heighest = judge3;
		if (judge4 > judge3) //if judge4 is greater than judge3 it assign judge4 to heighest
			heighest = judge4;
		else if (judge5 > judge3)//if judge5 is greater than judge3 it assign judge5 to heighest
			heighest = judge5;
	}
	else if (judge4 > judge1)// else if judge4 is greater than judge1 it assign judge4 to heighest
	{
		heighest = judge4;
		if (judge5 > judge4)//if judge5 is greater than judge4 it assign judge to heighest
			heighest = judge5;
	}
	else if (judge5 > judge1) // else if judge5 is greater than judge1 it assign judge5 to heighest
		heighest = judge5;

	return heighest; //returns heighest
}

When I run it it gives me the error: Run-Time Check Failure #3 - The variable 'judge4' is being used without being initialized. on line 75. the input file starsearch.dat will contain
2
Larry 6 5 7 8 4
Paul 10 5 8 7 6

and after the program runs successfully
results.dat will have

larry 6
Paul 7

On line 57...Try initializing your double variables to 0.0

Based on the posted code I'm surprised it's only flagging "judge4"; it should be flagging 4/5 of your judge values every time.

You may want to re-evaluate your algorithm a little. You really don't want to CalcScore until you've retrieved all 5 scores. As it stands now, you're re-calculating the average score after each individual judge's score is retrieved.

On line 57...Try initializing your double variables to 0.0

That will address the initialization issue, but there is a bigger algorithmic issue in play here that is partially to blame for it.

I get the wrong result. the value of the variables to back to zero when they are passed to CalcScore

Based on the posted code I'm surprised it's only flagging "judge4"; it should be flagging 4/5 of your judge values every time.

You may want to re-evaluate your algorithm a little. You really don't want to CalcScore until you've retrieved all 5 scores. As it stands now, you're re-calculating the average score after each individual judge's score is retrieved.


That will address the initialization issue, but there is a bigger algorithmic issue in play here that is partially to blame for it.

its for an assignment and the requirement is that i call getJudgeData() per score. and it does also say the same thing for judge5 sometimes. Do you know what the problem is

Based on the posted code I'm surprised it's only flagging "judge4"; it should be flagging 4/5 of your judge values every time.

You may want to re-evaluate your algorithm a little. You really don't want to CalcScore until you've retrieved all 5 scores. As it stands now, you're re-calculating the average score after each individual judge's score is retrieved.


That will address the initialization issue, but there is a bigger algorithmic issue in play here that is partially to blame for it.

no i'm not it doesnt send the scores to CaclScore until counter == 4

Like I mentioned earlier in response to gerard, it's an algorithmic issue. You're attempting to do all the right things, but you're not not doing them in the correct order and you're not attempting them in a correct fashion. Because you're not doing them in the correct order and fashion, you're getting messed up by scoping rules.

I think you should start by paying closer attention to your variable names. It appears that your variable "loopnum" is the number of data groups contained in the file (a.k.a. the number of contestants/records). I think I would rename it to something more relevant like "contestants" or "numberOfContestants". That way it's easier to understand what it represents when you go back and look at it a few weeks (or even months or years) down the road. This same suggestion applies across the board; look closely at all your variables and make sure their names make sense for what you're trying to accomplish.

... the requirement is that i call getJudgeData() per score...

Which it appears that you have done, but you've taken it to the extreme. The problem is the implementation of the function is not organized properly. This is where your issues are coming from.

The issue is that you're calling sub-functions from within getJudgeData(), which in-and-of itself is fine. Where the issue creeps in is your timing; you're neither declaring your variables correctly nor at the proper time and your also calling those other functions at the wrong times. Because of this, you have variable identifiers (names) coming into an going out of scope repeatedly. This cycle of in-out-in-out is causing you to think that a variable exists and is initialized when, in fact, it's not.

To begin to correct this, you have to go all the way back to your main(). Your inner for loop in your main() is really not necessary, and it's partially where your problem is coming from because you are using its control variable in a way that it really shouldn't be. Get rid of it. You should be able to accomplish the entire job from just the one loop within main(). By getting rid of it, it forces you to redesign the getJudgeData() function and I'd almost be willing to bet you'll see how much simpler it will become.

no i'm not it doesnt send the scores to CaclScore until counter == 4

Either way, the simple fact is, once CalcScore() is called, you've been in and out of the function at least 4 times causing all the variables to be destroyed and re-declared several times over. The information that should be contained in judge1 thru judge4 is long-gone. Without the use of the static keyword, that data doesn't persist from one execution of the function to the next.

Like I mentioned earlier in response to gerard, it's an algorithmic issue. You're doing all the right things, but you're not doing them in the correct order. Because you're not doing them in the correct order, you're getting messed up by scoping rules.

I think you should start by paying closer attention to your variable names. It appears that your variable "loopnum" is the number of data groups contained in the file (a.k.a. the number of contestants). I think I would rename it to something more relevant like "contestants" or "numberOfContestants". That way it's easier to understand when you go back and look at it a few weeks (or even months or years) down the road.


Which it appears that you have done, but the implementation of the function is not organized properly. This is where your issues are coming from.

The problem is you're calling sub-functions from within getJudgeData(), which in-and-of itself is fine, but you're not declaring your variables correctly and your calling those other functions at the wrong times. Because of this, you have variable identifiers (names) coming into an going out of scope repeatedly. This cycle of in-out-in is causing you to think that a variable exists and is initialized when, in fact, it's not.

Your inner for loop in your main() is really not necessary, and it's partially where your problem is coming from because you are using its control variable in a way that it really shouldn't be. Get rid of it. You should be able to accomplish the entire job from just the one loop within main(). By getting rid of it, it forces you to redesign the getJudgeData() function and I'd almost be willing to bet you'll see how much simpler it will become.


Either way, the simple fact is, once CalcScore() is called, you've been in and out of the function at least 4 times causing all the variables to be destroyed and re-declared several times over. The information that should be contained in judge1 thru judge4 is long-gone. Without the use of the static keyword, that data doesn't persist from one execution of the function to the next.

gotchya. for the most part I personally agree with you. But its what the assignment wants me to do. But thank u for reminding me that the variables values erase everytime i exit the function. do u think I should just make them static

Like I mentioned earlier in response to gerard, it's an algorithmic issue. You're attempting to do all the right things, but you're not not doing them in the correct order and you're not attempting them in a correct fashion. Because you're not doing them in the correct order and fashion, you're getting messed up by scoping rules.

I think you should start by paying closer attention to your variable names. It appears that your variable "loopnum" is the number of data groups contained in the file (a.k.a. the number of contestants/records). I think I would rename it to something more relevant like "contestants" or "numberOfContestants". That way it's easier to understand what it represents when you go back and look at it a few weeks (or even months or years) down the road. This same suggestion applies across the board; look closely at all your variables and make sure their names make sense for what you're trying to accomplish.


Which it appears that you have done, but you've taken it to the extreme. The problem is the implementation of the function is not organized properly. This is where your issues are coming from.

The issue is that you're calling sub-functions from within getJudgeData(), which in-and-of itself is fine. Where the issue creeps in is your timing; you're neither declaring your variables correctly nor at the proper time and your also calling those other functions at the wrong times. Because of this, you have variable identifiers (names) coming into an going out of scope repeatedly. This cycle of in-out-in-out is causing you to think that a variable exists and is initialized when, in fact, it's not.

To begin to correct this, you have to go all the way back to your main(). Your inner for loop in your main() is really not necessary, and it's partially where your problem is coming from because you are using its control variable in a way that it really shouldn't be. Get rid of it. You should be able to accomplish the entire job from just the one loop within main(). By getting rid of it, it forces you to redesign the getJudgeData() function and I'd almost be willing to bet you'll see how much simpler it will become.


Either way, the simple fact is, once CalcScore() is called, you've been in and out of the function at least 4 times causing all the variables to be destroyed and re-declared several times over. The information that should be contained in judge1 thru judge4 is long-gone. Without the use of the static keyword, that data doesn't persist from one execution of the function to the next.

Thank You so very very much.

gotchya. for the most part I personally agree with you. But its what the assignment wants me to do....

In that case, are you familiar with arrays? This program seems like a really good candidate for them.

...But thank u for reminding me that the variables values erase everytime i exit the function. do u think I should just make them static

Unless you've been shown static, I would say no. For a situation like this, that would be a very "hackish" solution. You're far better off addressing the design/algorithm issues than you are forcing a bad design to work. Using static here would be like trying to put a square peg in a round hole.

In that case, are you familiar with arrays? This program seems like a really good candidate for them.

Unless you've been shown static, I would say no. For a situation like this, that would be a very "hackish" solution. You're far better off addressing the design/algorithm issues than you are forcing a bad design to work. Using static here would be like trying to put a square peg in a round hole.

Hey it worked at least

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.