#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