star search program

Pages: 12
@kfmfe
just to be clear on what my current code looks like

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
136
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

string contestantName( ifstream & );
double getJudgeData( ifstream & );
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 = 0; count < 2; count++){

	string contestantName(contName);

	getJudgeData(inputFile);

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

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


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(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;
}


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


go to town folks
like I said, on Line 26 you have attempted to call the forward declaration you have on Line 10, but there is no implementation for Line 10
to be honest im still new to C++ and even thought i can type out these complex problems i can not make sense of the vocabulary of the language in terms of what kfmfe says above with "implementation"

im just really tired and frustrated with this code and would like just a straight forward answer meant for the commoner that does not understand this at all because my brain is physically tapped out of ideas
Last edited on
this is a forward declaration:
int sum( int a, int b );

this is an implementation:
int sum( int a, int b ) { return a+b; }
okay i put a return at the end of getJudgeData()
1
2
3
4
5
6
7
8
9
10
11
12
double getJudgeData(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;
		return score1+score2+score3+score4+score5;
}

i hope that this is right and if i did not say it before thank you for your help
Last edited on
you didn't quite get it right.

you need to do this:

1
2
3
4
double getJudgeData( ifstream& ifs )
{
// fill out the insides here with something like Line 10 from your post above
}
so basically take your line 1 and then put my lines 3, 4, 10 and 11 inside the function

thats what i understand or just add line 10 only into the function?

i also seem to be getting the same error message for my CalcScore function
1
2
3
4
5
6
7
8
9
double CalcScore (double &totalScore){

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

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

	return totalScore;
}
Last edited on
the code now works somewhat i just cant seem to get the right information sent to my output file

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

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

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

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

	for (int count = 0; count < 2; count++){

	string contestantName(contName);

	getJudgeData(inputFile);

	CalcScore(totalScore);

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


string contestantName (string contName){
	ifstream inputFile;
	const int size = 81;
	char name[size];
	inputFile.open("starsearch.dat");

	contName = name;

	inputFile >> contName;
	
	return contName;
}


double getJudgeData( ifstream& ifs ){

	ifstream inputFile;
	inputFile.open("starsearch.txt");
        inputFile>>score1>>score2>>score3>>score4>>score5;
		return 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.0);

	return totalScore;
}
Last edited on
this is almost done its just that whenever i compile the program i do not get the correct output in my output file
you need to debug your code - you have three choices:

1. sprinkle cerr << "messages" << endl; throughout your code to see what is going on
2. learn how to use a debugger and step through your code
3. write a unit-test to ensure your code satisfies requirements

3. is best, 2. is good, and 1. should get the job done
i would like to use the 1st option but where would i put those pieces of code just anywhere or in specific spots

the 2nd option would be great as well but where would i find a debugger would it be somewhere in visual studio C++ 2010

the 3rd option would be great if i knew how to do it (i am hopeless)
You put them starting from where you know something is wrong.
Then you put them farther and farther up closer to where you start in main().
Eventually some of them will print out the right results - pay attention to the transition from right results to wrong results.

The debugger is built into Visual Studio C++ 2010 - you should be able to pick Run Debugged or Step Through Code in one of the menus.
i have actually ran the debugger and it seems that the values of all of my variables does not change to what they are supposed to be (which is the information that is in the starsearch.dat input file
as you step through the debugger, you need to:

- look at your variable values
- look at what your program is doing
- evaluate whether the computer is doing what you want it to do

if there are surprises, you have a bug - the machine is stupid: it can only do what you tell it to do
the still not working code that has been debugged and gone over about 20 times now so any help to get this done even if you have to run it your self would be fantastic

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

string contestantName(ifstream & );
double getJudgeData(ifstream &);
double findLowest(double &lowestScore);
double findHighest(double &highestScore);
double CalcScore(double &totalScore);

string contName;
double score1, score2, score3, score4, score5, lowestScore, highestScore, totalScore;
ifstream inputFile;
ofstream outputFile;


int main(){

	inputFile.open("starsearch.dat");

	for (int count = 0; count < 2; count++){

	string contestantName(contName);
	for (int count = 0; count < 5; count++){

	getJudgeData(inputFile);

	CalcScore(totalScore);

	outputFile.open ("results.dat");
	outputFile << contName << totalScore;
		}
	}
	system ("pause");
}


string contestantName (string contName){
	
	const int size = 81;
	char name[size];

	contName = name;

	inputFile >> contName;
	
	return contName;
}


double getJudgeData( ifstream& ifs ){
        inputFile>>score1>>score2>>score3>>score4>>score5;
		return 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.0);

	return totalScore;
}
Last edited on
You don't need global variables. Limit their scope to main.

1
2
3
4
5
6
7
8
9
	for (int count = 0; count < 5; count++){
	getJudgeData(inputFile); //how many times is this executed?
//...
double getJudgeData( ifstream& ifs ){
	ifstream inputFile;
	inputFile.open("starsearch.txt");
        inputFile>>score1>>score2>>score3>>score4>>score5; //how many variables are you reading?
		return score1,score2,score3,score4,score5; //the return does not work in that way (it will return score5 only)
}

Why you are not catching the returned values?
urgent help is needed

the variables are supposed to be read from a file and be treated as input in the program to get an output that will also be placed in a output file

Input file sample
2
Sam 9.5 10 5.8 7.9 8.0
Dave 10 5.2 6.8 9.2 8.0

Output file sample
Sam 8.5
Dave 8.0

most of the directions are on the first page if you need to remind yourself
Last edited on
Topic archived. No new replies allowed.
Pages: 12