Function Compiling Errors

Hello! I'm new here to the forums, and I'm currently taking Intro to C++ as a requisite for my Associate's and currently we're learning about functions. I understand how they work but this program doesn't want to agree with me. I've tried fixing the errors but every time I do, new ones pop up. Here is my program.

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
//The purpose of this program was to ask the user to input 5 scores using
//functions to find the lowest and highest scores, and then dropping them
//to have only three left, averaging them and then display the results.
#include <iostream>
using namespace std;

//Function Prototypes
double getJudgeData(); //Function for inputing values and validating that theyr'e in range.
double findLowest();   //Finds the lowest score and returns value for later use.
double findHighest();  //Finds the highest score and returns value for later use.
double calcScore();    //Uses and calculates returned values for average score.

//Main Function and displays functions' results.
int main()
{
	double lowestScore, highestScore, totalScore;
	getJudgeData();
	lowestScore = findLowest();
	highestScore = findHighest();
	totalScore = calcScore();
	cout<<"The contestant's total score is: "<<totalScore<<endl;
return 0;
}

//Function for user input.
double getJudgeData()
{
	double score1, score2, score3, score4, score5, total;
	cout<<"Please enter your rating."<<endl;
	cin>>score1>>score2>>score3>>score4>>score5;

	//It validates that the numbers are within range.
	while(score1 < 0 || score1 > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score1;
	}
	while(score2 < 0 || score2 > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score2;
	}
	while(score3 < 0 || score3 > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score3;
	}
	while(score4 < 0 || score4 > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score4;
	}
	while(score5 < 0 || score5 > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score5;
	}
	
	total = score1 + score2 + score3 + score4 + score5;
return total;
} //Return? (Trying to return score1-score5 but don't know how. Total as temp.)

//This function is to find the lowest score and return the value.
double findLowest(double score1, double score2, double score3, double score4, double score5)
{
	double lowestScore, total;
	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 = score5;
	}
	else if(score5 <= score1 && score5 <= score2 && score5 <= score3 && score5 <= score4)
	{
		lowestScore = score5;
	}
return lowestScore;
}

//This function is to find the highest score, and returns the value.
double findHighest(double score1, double score2, double score3, double score4, double score5)
{
	double highestScore;
	if(score1 >= score2 && score1 >= score3 && score1 >= score4 && score1 >= score5)
	{
		highestScore = score1;
	}
	if(score2 >= score1 && score2 >= score3 && score2 >= score4 && score2 >= score5)
	{
		highestScore = score2;
	}
	if(score3 >= score1 && score3 >= score2 && score3 >= score4 && score3 >= score5)
	{
		highestScore = score3;
	}
	if(score4 >= score1 && score4 >= score2 && score4 >= score3 && score4 >= score5)
	{
		highestScore = score4;
	}
	if(score5 >= score1 && score5 >= score2 && score5 >= score3 && score5 >= score4)
	{
		highestScore = score5;
	}
return highestScore;
}

//Function to drop highest and lowest scores and average out the total score. Then returns value to main.
double calcScore(double total, double lowestScore, double highestScore)
{
	double totalScore;
	totalScore = (total - (lowestScore + highestScore)) / 3;
return totalScore;
}


currently, the error that only shows up is:
homework5.cpp: In function ‘int main()’:
homework5.cpp:19: error: ‘total’ was not declared in this scope

I have a feeling this a simple logical error, but I can't put my finger on it. Also, I'd like to know how can I return multiple values within the getJudgeData() function, if even, possible.
EDIT: This is being written and compiled in Linux-based OS; Red Hat (or CentOS)
Last edited on
Hmm... this code produced linker errors for me, not compile errors. Are you sure both your code and error message are up-to-date in your original post?

In any case, you'll want your function declarations (function 'prototypes') on lines 8-11 to match the actual function definitions at the bottom of your code (lines 26, 64, 91, and 118). This means including the argument types that you'll be passing in. Right now, your function prototypes declare functions that don't take any arguments.
Last edited on
Okay so there's 2 easy ways I see for doing this.
The first is usually frowned upon and for good reason but for something as simple as this it don't really matter, this method is to declare your variables in the global scope (same place where your functions are declared).

That way you pass any data between any functions at all, however remember that this is usually frowned upon because these variables can be accessed by ANY part of the program, as I said it don't really matter with things like this but once you get bigger you really don't wanna do it.

1
2
3
4
5
6
7
8
//Declaring 5 double variables
double v1, v2, v3, v4, v5;
//OR declare a double array
double var[5];

int main(){
    //...
}


The second way I see is to keep all your variable in the main as they are but actually just use one array that can hold 5 elements (sorry if you don't know about arrays yet but they'd be ideal here).
Then you'd setup your functions like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double[5] getJudgeData(); //forget if this'll work, if not then replace the double[5] with double*
//findLowest/Highest are fine as is but your prototypes must state all the argument types too, i.e.
double getLowest(double, double, double, double, double);
//Or you can shorten this to an array too
double getLowest(double in[5]);
//Do same for calcScore but with 3 in the square brackets if you use arrays

int main(){
    double vals[5] = getJudgeData();
    double lowest = findLowest(vals); //this is how you'd call for findLowest(double in[5])
    double highest = findHighest(vals[0], vals[1], vals[2], vals[3], vals[4]); //this is how you'd call for findHighest(double, double, double, double, double)
    double totalScore = calcScore(vals); //gone back to this form cos I'm lazy, I just did it above to demonstrate
}

//Inside getJudgeData replace the 5 variable with one array
double[5] getJudgeData(){
    double var[5] = {0};
    cin >> var[0] >> var[1] >> var[2] >> var[3] >> var[4];
    //rest of function is same if you just replace the right things with the array notation
    return var; //returns the full array of 5 values
}


I hope this helps because although I know what I'm talking about I have a tendency screw up my explanations beyond all understanding XD
I'm not sure if I'm getting this correct but for the array in double[5] getJudgeData, do you have to have your variable (scores in my case) or will it do it by itself? Say like this?

double score[5] getJudgeData();

Or am I missing something? XD
double[5] getJudgeData(); //forget if this'll work, if not then replace the double[5] with double*

No, it won't work. Neither will returning a pointer to a local array which will no longer be in scope when you go to use the pointer.

What you could do is:

1
2
3
4
5
void getJudgeData(double * data, unsigned capacity)
{
    for ( unsigned i=0; i<capacity; ++i )
        std::cin >> data[i] ;
}


called like so:

1
2
3
4
5
6
int main()
{
    double vals[5] ;
    getJudgeData(vals, 5);
    // ...
}
Last edited on
Ok I've fixed up the code to look like this now:
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
//Program:--------.cpp
//-------
//-------

#include <iostream>
using namespace std;

//Function Prototypes
double score[5];
double getJudgeData();
double findLowest(double score[5]);
double findHighest(double score[5]);
double calcScore(double[5], double, double);

//Main Function and displays functions' results.
int main()
{
	double lowestScore, highestScore, totalScore;
	double score[5] = getJudgeData();
	lowestScore = findLowest(score);
	highestScore = findHighest(score);
	totalScore = calcScore();
	cout<<"The contestant's total score is: "<<totalScore<<endl;
return 0;
}

//Function for user input.
double[5] getJudgeData()
{
	double score[5] = {0};
	cout<<"Please enter your rating."<<endl;
	cin>>score[0]>>score[1]>>score[2]>>score[3]>>score[4];

	//It validates that the numbers are within range.
	while(score[0] < 0 || score[0] > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score[0];
	}
	while(score[1] < 0 || score[1] > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score[1];
	}
	while(score[2] < 0 || score[2] > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score[2];
	}
	while(score[3] < 0 || score[3] > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score[3];
	}
	while(score[4] < 0 || score[4] > 10)
	{
		cout<<"Invalid number, enter a rating between 0 through 10 only."<<endl;
		cin>>score[4];
	}

return score;
}

//This function is to find the lowest score and return the value.
double findLowest(score)
{
	double lowestScore;
	if(score[0] <= score[1] && score[0] <= score[2] && score[0] <= score[3] && score[0] <= score[4])
	{
		lowestScore = score[0];
	}
	else if(score[1] <= score[0] && score[1] <= score[2] && score[1] <= score[3] && score[1] <= score[4])
	{
		lowestScore = score[1];
	}
	else if(score[2] <= score[0] && score[2] <= score[1] && score[2] <= score[3] && score[2] <= score[4])
	{
		lowestScore = score[2];
	}
	else if(score[3] <= score[0] && score[3] <= score[1] && score[3] <= score[2] && score[3] <= score[4])
	{
		lowestScore = score[3];
	}
	else if(score[4] <= score[0] && score[4] <= score[1] && score[4] <= score[2] && score[4] <= score[3])
	{
		lowestScore = score[4];
	}
return lowestScore;
}

//This function is to find the highest score, and returns the value.
double findHighest(score)
{
	double highestScore;
	if(score[0] >= score[1] && score[0] >= score[2] && score[0] >= score[3] && score[0] >= score[4])
	{
		highestScore = score[0];
	}
	if(score[1] >= score[0] && score[1] >= score[2] && score[1] >= score[3] && score[1] >= score[4])
	{
		highestScore = score[1];
	}
	if(score[2] >= score[0] && score[2] >= score[1] && score[2] >= score[3] && score[2] >= score[4])
	{
		highestScore = score[2];
	}
	if(score[3] >= score[0] && score[3] >= score[1] && score[3] >= score[2] && score[3] >= score[4])
	{
		highestScore = score[3];
	}
	if(score[4] >= score[0] && score[4] >= score[1] && score[4] >= score[2] && score[4] >= score[3])
	{
		highestScore = score[4];
	}
return highestScore;
}

//Function to drop highest and lowest scores and average out the total score. Then returns value to main.
double calcScore(score, lowestScore, highestScore)
{
	double totalScore;
	totalScore = ((score[0] + score[1] + score[2] + score[3] + score[4]) - (lowestScore + highestScore)) / 3;
return totalScore;
}


Does it look correct so far? Because now I only have these errors, what do they mean and what do I need to do to fix them?

each error corresponds to the following line number

[centos@localhost homework]$ g++ -g hw5.cpp -o hw5
hw5.cpp: In function ‘int main()’:
hw5.cpp:19: error: array must be initialized with a brace-enclosed initializer
hw5.cpp:13: error: too few arguments to function ‘double calcScore(double*, double, double)’
hw5.cpp:22: error: at this point in file

hw5.cpp: At global scope:
hw5.cpp:28: error: expected unqualified-id before ‘[’ token
Last edited on
No, it won't work. Neither will returning a pointer to a local array which will no longer be in scope when you go to use the pointer.

Oh indeed... I'm making many simple mistakes myself here so sorry OP for the confusion.

The best idea would be to initialise the variable array in your main function and pass it to the getJudgeData(double in[5])

Again sorry for the confusion... It's been a while since I've needed something like this for local/automatic variables XD
Topic archived. No new replies allowed.