star search program

Pages: 12
this is a list of directions that i have received for an assignment but some points i do not understand:

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

the bold text are the parts of the assignment that make no sense to me can someone explain these as clearly as possible
Last edited on
is it that much trouble to ask for some explanations
If file IO is your issue you should take a look at http://www.cplusplus.com/doc/tutorial/files
That first part is possibly the most frustrating way I have EVER seen to be told to read a file. You need to have that function "contestantName(...)" except a ifstream argument, read a line from the file and return the data to main. Do this in a loop.

This second part is even confusing me. Do you have the text file that you are supposed to be reading from?
its a text file that i am supposed to create in which the programer (me) decides how many contestants and their scores

an example would look like this:

2
Dan 10 9.5 8.6 9.9 10
Steve 10 9.2 5.8 8.2 5.6

that is pretty much it and yes it is very confusing


here is my code so far for this horrendous problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

string contestantName(string);
void getJudgeData(string&, double&, double&, double&, double&, double&);
double findLowest(double &lowestScore);
double findHighest(double &highestScore);
void CalcScore(string&,double&, double&, double&, double&, double&, double&);

string contName;
double score1, score2, score3, score4, score5, lowestScore, highestScore, totalScore;

int main(){
	
	ifstream inputFile;
	inputFile.open("starsearch.dat");

	getJudgeData(contName, score1, score2, score3, score4, score5);

	CalcScore(contName,score1,score2,score3,score4,score5,totalScore);

}


string contestantName (ifstream &inputFile){
	string contName;

	inputFile >> contName;
	
	return contName;
}


void getJudgeData(string &contName, double &score1, double &score2, double &score3, double &score4, double &score5){

	ifstream inputFile;
	inputFile.open("starsearch.txt");
	if(!inputFile)
	    {
    cout<<"Error!File not found.";
	     exit(1);
	    }
	     
    for(int i=1;i<=2;i++)
    {
        inputFile>>contName;
        inputFile>>score1>>score2>>score3>>score4>>score5;
	    }
}


double findLowest(double &lowestScore){

if ((score1 <= score2) && (score1 <= score3) && (score1 <= score4) && (score1 <= score5))
		{
			lowestScore = score1; 
		}
		
		else if ((score2 <= score1) && (score2 <= score3) && (score2 <= score4) && (score2 <= score5))
		{
			lowestScore = score2; 
		}

		else if ((score3 <= score1) && (score3 <= score2) && (score3 <= score4) && (score3 <= score5))
		{
			lowestScore = score3; 
		}

		else if ((score4 <= score1) && (score4 <= score2) && (score4 <= score3) && (score4 <= score5))
		{
			lowestScore = score4;
		}

		else if ((score5 <= score1) && (score5 <= score2) && (score5 <= score3) && (score5 <= score4))
		{
			lowestScore = score5; 
		}
return lowestScore;

}


double findHighest(double &highestScore){

if ((score1 >= score2) && (score1 >= score3) && (score1 >= score4) && (score1 >= score5))
		{
			highestScore = score1; 
		}
		
		else if ((score2 >= score1) && (score2 >= score3) && (score2 >= score4) && (score2 >= score5))
		{
			highestScore = score2; 
		}

		else if ((score3 >= score1) && (score3 >= score2) && (score3 >= score4) && (score3 >= score5))
		{
			highestScore = score3; 
		}

		else if ((score4 >= score1) && (score4 >= score2) && (score4 >= score3) && (score4 >= score5))
		{
			highestScore = score4;
		}

		else if ((score5 >= score1) && (score5 >= score2) && (score5 >= score3) && (score5 >= score4))
		{
			highestScore = score5; 
		}
return highestScore;

}


void CalcScore (double &totalScore){

	double findLowest(double lowestScore);
	double findHighest(double highestScore);

	totalScore =(((score1+score2+score3+score4+score5)-(lowestScore+highestScore))/3);


}


NOTE some things are not finished yet
Last edited on
getJudgeData: This function will be called five times per contestant. (one for each score)
The file will be open only once (in main). Basically is asking for 2 function, one that reads a string and one that reads a double.

By the way
1
2
//double score1, score2, score3, score4, score5;
double score[5]; //an array 
@ne555 i havent learned how to do arrays yet and how would i go about calling a function for x amount of times a count variable like the one inside of the getJudgeData function just with a call ?
here is a somewhat final draft of my code so im only having a linking problem with my CalcScore function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

string contestantName(string);
void getJudgeData(string&, double&, double&, double&, double&, double&);
double findLowest(double &lowestScore);
double findHighest(double &highestScore);
void CalcScore(string&,double&, double&, double&, double&, double&, double&);

string contName;
double score1, score2, score3, score4, score5, lowestScore, highestScore, totalScore;

int main(){
	
	ifstream inputFile;
	inputFile.open("starsearch.dat");

	for (int count = 1; count < 2;count++){
	getJudgeData(contName, score1, score2, score3, score4, score5);

	CalcScore(contName,score1,score2,score3,score4,score5,totalScore);

	ofstream outputFile;
	outputFile.open ("results.dat");
	outputFile << contName << totalScore;
	outputFile.close();
	}
	system("pause");
  return 0;
}


string contestantName (ifstream &inputFile){
	string contName;

	inputFile >> contName;
	
	return contName;
}


void getJudgeData(string &contName, double &score1, double &score2, double &score3, double &score4, double &score5){

	ifstream inputFile;
	inputFile.open("starsearch.txt");
	if(!inputFile)
	    {
    cout<<"Error!File not found.";
	     exit(1);
	    }
	     
    for(int i=1;i<=2;i++)
    {
        inputFile>>contName;
        inputFile>>score1>>score2>>score3>>score4>>score5;
	    }
}


double findLowest(double &lowestScore){

if ((score1 <= score2) && (score1 <= score3) && (score1 <= score4) && (score1 <= score5))
		{
			lowestScore = score1; 
		}
		
		else if ((score2 <= score1) && (score2 <= score3) && (score2 <= score4) && (score2 <= score5))
		{
			lowestScore = score2; 
		}

		else if ((score3 <= score1) && (score3 <= score2) && (score3 <= score4) && (score3 <= score5))
		{
			lowestScore = score3; 
		}

		else if ((score4 <= score1) && (score4 <= score2) && (score4 <= score3) && (score4 <= score5))
		{
			lowestScore = score4;
		}

		else if ((score5 <= score1) && (score5 <= score2) && (score5 <= score3) && (score5 <= score4))
		{
			lowestScore = score5; 
		}
return lowestScore;

}


double findHighest(double &highestScore){

if ((score1 >= score2) && (score1 >= score3) && (score1 >= score4) && (score1 >= score5))
		{
			highestScore = score1; 
		}
		
		else if ((score2 >= score1) && (score2 >= score3) && (score2 >= score4) && (score2 >= score5))
		{
			highestScore = score2; 
		}

		else if ((score3 >= score1) && (score3 >= score2) && (score3 >= score4) && (score3 >= score5))
		{
			highestScore = score3; 
		}

		else if ((score4 >= score1) && (score4 >= score2) && (score4 >= score3) && (score4 >= score5))
		{
			highestScore = score4;
		}

		else if ((score5 >= score1) && (score5 >= score2) && (score5 >= score3) && (score5 >= score4))
		{
			highestScore = score5; 
		}
return highestScore;

}


double CalcScore (double &totalScore){

	double findLowest(double lowestScore);
	double findHighest(double highestScore);

	totalScore = (((score1+score2+score3+score4+score5)-(lowestScore+highestScore))/3);

	return totalScore;
}
Last edited on
i am still having trouble what my instructions should be in terms of my judgedata function with the loop that needs to be implemented i it
The loop is not implemented in the function, the function is called in a loop in main(). Also, the functions are incorrect; according to your instructions, they are supposed to take a reference to an ifstream and return a string (in the case of contestantName()) or a double (in the case of getJudgeData()). One parameter, and ifstream reference, and one return value.
Last edited on
for input:
2
Dan 10 9.5 8.6 9.9 10
Steve 10 9.2 5.8 8.2 5.6


what is the output supposed to look like?
@LB this is what i understand from what u said

i should take the loop out of the getJudgeData function and keep the loop that is in main

another thing is that i really do not understand the whole ifstream process or how its to be done the only one that makes sense is the fstream data type

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string contestantName (string contName){
	ifstream inputFile;
	const int size = 81;
	char name[size];
	inputFile.open("starsearch.dat");

	contName = name;

	inputFile >> contName;
	
	return contName;
}


void getJudgeData(string &contName, double &score1, double &score2, double &score3, double &score4, double &score5){

	ifstream inputFile;
	inputFile.open("starsearch.txt");
	if(!inputFile)
	    {
    cout<<"Error!File not found.";
	     exit(1);
	}
        inputFile>>score1>>score2>>score3>>score4>>score5;
}


this is the edited versions of those functions using your suggestion although i do not think that the getJudgeData function is right would it be the same way as in the previous function or something different
Last edited on
the out put is supposed to look like this:

Dan "final score"
Steve "final score"
the final score is calculated from the 5 scores per contestant the sum of all those scores then subtracting the highest and lowest scores among them then averaging out the remaining scores

that is where my CalcScore function comes in
so you mean this?
Dan 9.80
Steve 7.73
yes exactly but the output has to be sent to a file as well
Last edited on
1
2
for (int count = 1; count < 2;count++){
	getJudgeData(contName, score1, score2, score3, score4, score5);
That is not a loop, because is executed only one time.
If you don't use an array for the scores, either override the value in every pass, or don't use a loop.

These are the prototypes of the function that you should implement (according to the assignment)
1
2
double getJudgeData( ifstream & );
string contestantName( ifstream & );
@ne555

ah i see i changed the code to fit the in sight that you gave me and i git this error message from my compiler

main.obj : error LNK2019: unresolved external symbol "double __cdecl getJudgeData(class std::basic_ifstream<char,struct std::char_traits<char> > &)" (?getJudgeData@@YANAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _main
Last edited on
My guess, you didn't respect the prototype. Post the function, and the calls in main.
too bad C++ is required (along with your nasty teacher requirements trying to test if you understand references, etc...), because it's a one-liner on UNIX:

echo -e 'LIST="$(awk '\''END{ for (i=2;i<=NR;++i) printf "%d ",i; }'\'' < /tmp/input.txt)"\nfor i in $LIST;\tdo \nsed -n "${i},${i}p" < /tmp/input.txt | xargs -n 1 | sort -n | xargs -n 6 | awk '\''{ print $1,($3+$4+$5)/3; }'\''\ndone ' > /tmp/worker.sh && chmod +x /tmp/worker.sh && /tmp/worker.sh

/tmp/input.txt
2
Dan 10 9.5 8.6 9.9 10
Steve 10 9.2 5.8 8.2 5.6


/tmp/result.txt
Dan 9.8
Steve 7.73333


it looks scary like this, but if you look inside /tmp/worker.sh, it's not too bad (if you know a little UNIX):
1
2
3
4
LIST="$(awk 'END{ for (i=2;i<=NR;++i) printf "%d ",i; }' < /tmp/input.txt)"
for i in $LIST;	do 
sed -n "${i},${i}p" < /tmp/input.txt | xargs -n 1 | sort -n | xargs -n 6 | awk '{ print $1,($3+$4+$5)/3; }'
done 


might have to call it a 4-liner in shell - but still much less code than C++

OTOH, someone who is much better versed than I in UNIX (not hard to find) could simplify this code some more, I am sure!
Last edited on
@UrbnCampr02

that link error is caused by a call to:

double getJudgeData( ifstream & );

but no implementation
Pages: 12