Star Search Program

Hey everyone,
I have a program named Star Search that I must do for my Computer Science class. I'm stuck because I can't seem to get the do while loop to function properly. Basically when the answer is y I want the program to loop, when it's n I want it to system pause, when it's not n or y I want it to output a prompt then request another input. The program seems to be starting on line 76.

Thanks!

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

using namespace std;

int answer;
double s1 = 0.0, s2 = 0.0, s3 = 0.0, s4 = 0.0, s5 = 0.0;
double highest = 0.0, lowest = 0.0;

double getJudgesData(double &s1)
{
	cout << "Enter the judge's score: ";
	cin >> s1;
	while (s1 < 0.0 || s1 > 10.0)
	{
		cout << "Please enter a score between 0 and 10.\n";
		cout << "Please enter the judge's score: ";
		cin >> s1;
		return s1;
	}
}

double findLowest(double &lowest)
{
	if (s1 < s2)
	{
		lowest = s1;
	}
	else { lowest = s2; }
	if (s3 < lowest)
	{
		lowest = s3;
	}
	if (s4 < lowest)
	{
		lowest = s4;
	}
	if (s5 < lowest)
	{
		lowest = s5;
	}
	return lowest;
}

double findHighest(double &highest)
{
	if (s1 > s2)
	{
		highest = s1;
	}
	else { highest = s2; }
	if (s3 > highest)
	{
		highest = s3;
	}
	if (s4 > highest)
	{
		highest = s4;
	}
	if (s5 > highest)
	{
		highest = s5;
	}
	return highest;
}

void calcScore()
{
	findLowest(lowest);
	findHighest(highest);
	double total = s1 + s2 + s3 + s4 + s5;
	double average = (total - highest - lowest) / 3;
	cout << "Average score: " << fixed << setprecision(1) << average << endl;
}

char contestantCheck(int &answer)
{
	cout << "Do you want to enter another contestant? Please type y for yes or n for no.\nAnswer: ";
	cin >> answer;
	while (answer != 'n' || answer != 'y')
	{
		cout << "The only answers allowed are y for yes and n for no.\n";
		cin >> answer;
		return answer;
	}
}

int main()
{
	do
	{
		cout << "---------- Star Search ----------\n";
		cout << "Enter 5 scores for the contestant and I will drop the lowest and highest to find the average score.\n";
		getJudgesData(s1);
		getJudgesData(s2);
		getJudgesData(s3);
		getJudgesData(s4);
		getJudgesData(s5);
		calcScore();
		contestantCheck(answer);
	} while (answer == 'y');
		
	system("pause");
	return 0;

}
Last edited on
Well, the good news is this code seems fine (I compiled it). Your error type seems a little more unusual. Have you switched over to another IDE? What IDE are you currently using? If it's Visual Studio, are the files you're accessing directly inside the projects folder?
Last edited on
I compiled it online just now and it is working fine which is weird. I'll try to copy and paste the code to my MacBook and see if it has the same issue. The average is still coming out wrong though.

Edit: I've figured out everything else but the answer loop is confusing me.
Last edited on
You're off to a great start. You're getting tripped up because line 24 is legal syntax, but does something very different from what you intend:
double findHighest(highest), findLowest(lowest);
This actually defines two VARIABLES (not functions) called findHighest and findLowest. The variables are initialized from highest and lowest respectively. You should replace calcScore() with this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double findHighest()
{
    // find and return the highest of the 5 scores
}

double findLowest()
{
    // find and return the lowest of the 5 scores
}


void
calcScore()
{
    double lowest = findLowest();
    double highest = findHighest();
    double total = s1 + s2 + s3 + s4 + s5;
    // double average = (total - findHighest - findLowest) / 3;
    double average = (total - highest - lowest) / 3;
    cout << "Average score: " << average;
}


This defines the findLowest() and findHighest() functions. You will need to write the code to find them.

Have you learned about arrays? Using an array of scores for this assignment would make it a lot easier.

Also, note that findHighest() and findLowest() are returning doubles instead of int as the assignment asks. I believe that this is an oversight in the assignment but you should confirm with the professor.

Once you get this working, don't forget to modify main() to keep asking for scores for more performers as the assignment requires.
if you want to make the program stop when the user enter 'n' just use the "return 0;" or the exit function after the statement.

I'm doing the same project, and I'm unable to redo the whole process when the user enters y. can you show me how you did it?
Topic archived. No new replies allowed.