2
votes

This is a practice assignment from my textbook to calculate a score. It takes 7 scores and drops the highest and lowest.

I don't think there are syntax errors but I am getting an unresolved external symbol error. I looked for similar questions and it seemed that the problem may be with using a function but not defining it. I have defined all my functions, but probably incorrectly in either main or calculatescore. I am new to c++ and would appreciate any assistance in solving this issue. Thank you

This is the error I got on VisualStudio

error LNK2019: unresolved external symbol "float __cdecl calculateScore(float * const,int,float)" (?calculateScore@@YAMQAMHM@Z) referenced in function _main

#include <iostream>
using namespace std;

void printHeader(int judges);
void enterData (float scores[], int judges, float difficulty);
float calculateScore(float scores[], const int judges, float difficulty);
int findLeast(float scores[], const int judges);
int findMax(float scores[], const int judges);

int main () {
    const int judges = 7;
    float scores [judges];
    float difficulty = 0;
    float finalscore = calculateScore(scores, judges, difficulty);



printHeader (judges);
    enterData (scores, judges, difficulty);  // get user input
    findLeast(scores, judges); // find lowest score 
    findMax(scores, judges); // find highest score
    calculateScore (scores, judges, difficulty); // get final score
    cout << "The final score is " << finalscore << '\n';

    return 0;
}

void printHeader(const int judges) {
    cout << "This program calculates a divers score over" << judges << "judges"; 
}

void enterData(float scores[], const int judges, float difficulty) {
    for (int i = 0; i < judges; i++){
        cout <<"Enter score for judge " << i+1 << endl; 
        cin >> scores[i];
    }
    cout << "Enter difficulty: "<< endl;
    cin >> difficulty;
}

This is my function to calculate score that is called in main. Should it be a void function instead?

float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least) {
    float sum = 0;
        for (int i = 0; i < judges; i++) {
        sum += scores[i];
    }
    return sum - scores[least] - scores[maxScore] * difficulty * .6;
}


int findLeast(float scores[], const int judges) {
    int least = 0;
    for (int i = 1; i< judges; i++)
        if (scores[i] < scores[least])
            least = i;
    return least;
}

int findMax(float scores[], const int judges) {
    int maxScore = 0;
    for (int i = 1; i< judges; i++)
        if (scores[i] > scores[maxScore]) {
            maxScore = i; 
        }
    return  maxScore;
}
3

3 Answers

5
votes

You are calling calculateScore with args float * const,int,float from your main function, while the signature for the actual method is float scores[], const int judges, float difficulty, int maxScore, int least.

In summary, you are missing arguments in your call to the function in main(). The method requires an array scores[], an integer judges, a float difficulty, and an int maxScore, as well as an int least, but you've only given a float* (not the same as a float[]), an int, and another float.

Therefore you have not provided the correct type/number of arguments, and the compiler is telling you that it cannot find a method with those argument types.

A problem unrelated to your error is that in your method definition (of calculateScore) you are using maxScore as an index of scores, while maxScore appears to simply be the value of the maximum score. Therefore you can replace scores[maxScore] with simply maxScore. The same goes for least - it is a value, not an index, so you can use least instead of scores[least].

2
votes

This signature is for the function you've written: float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least)

and this is your prototype: float calculateScore(float scores[], const int judges, float difficulty);

They don't match is signatures.

First of all your should change your prototype to be:

float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least);

And as pointed allready, you should call your function with 5 varibleas of type: float [], const int, float, int, int instead of float [], const int, float

I think is what you want in your main:

int max = findMax(scores, judges);
int min = findLeast(scores, judges);
calculateScore (scores, judges, difficulty, max, min);
1
votes

Rewrite the code inside your main to the following:

  const int judges = 7;
  float scores [judges];
  float difficulty = 0;
  float finalscore;
   
  printHeader (judges);
  int leastTemp;
  int maxTemp;
  enterData (scores, judges, difficulty);  // get user input
  leastTemp = findLeast(scores, judges); // find lowest score 
  maxTemp = findMax(scores, judges); // find highest score
  finalscore = calculateScore (scores, judges, difficulty, maxTemp, leastTemp); // get final score
  cout << "The final score is " << finalscore << '\n';

  return 0;

This way you are putting the two missing parameters into the function calculateScore.