1

I am trying to make a talent show type voting program using functions.

I have the majority of it figured out. The program prompts you to enter a name, followed by five scores, if you type "Done" rather than a name, it will close. I'm using functions for a majority of the code to practice with them.

My big issue is that there could be an infinite amount of names (as many as the user enters) and I am unaware on how to add up all 5 scores per name, I do not know how to differentiate between them. The 5 scores will be averaged and the person with the highest average of (3 scores, dropping 2) will be the winner.

Side Note: I need to drop the highest and lowest score of each person, I believe I could figure it out but an example with a function of this would be helpful to someone who is new to them.

I've researched this a lot but I could not find any examples that are similar enough to mine (having a possibly infinite amount of contestants.)

Here is my code so far, the function at the bottom is me messing around with functions to get a hang of them and see if i can get any sums of scores from a name.

    #include <iostream>
#include <string>
using namespace std;

void validCheck();
void calcAvgScore();
void findHigh();
void findLow();


int main(){
    int judge = 1;
    double score = 0;
    string name;


    while (name != "done" || name != "Done"){
        cout << "Enter Contestant Name, if no more, type 'done': ";
        cin >> name;
        if (name == "done" || name == "Done"){ break; }
        for (judge = 1; judge < 6; judge++){
            cout << "Enter score " << judge << " ";
            validCheck();
        }
    }


    system("pause");
    return 0;


}



void validCheck(){
    double score;
    cin >> score;
    if (score < 1 || score > 10){
        cout << "Please Enter a score between 1 and 10: ";
        cin >> score;
    }
}

void calcAvgCheck(){
    double score = 0, value = 0;
    static int average;

    score += value
}
Ace Ebert
  • 71
  • 1
  • 2
  • 7

1 Answers1

0

Declare a string "winner", double "win_avg", double "avg" outside the while loop.

Have your validCheck() return the double value given as input (named score).

Declare a double array before your for loop (double[5] scores). Store each value returned from validCheck() into the array).

Call std::sort(std::begin(scores), std::end(scores)) to sort your scores ascending. Find the average (ignoring the max and min), and hold the max average as well as the names of the person with the max average.

#include <algorithm>    // std::sort
...
double validCheck();
...

int main(){
    string name;
    string winner;
    double win_avg;
    double avg;

    while (name != "done" || name != "Done"){
        cout << "Enter Contestant Name, if no more, type 'done': ";
        cin >> name;
        double scores[5];
        if (name == "done" || name == "Done"){ break; }
        for (int judge = 0; judge < 5; ++judge){
            cout << "Enter score " << judge << " ";
            scores[judge] = validCheck();
        }
        std::sort(std::begin(scores), std::end(scores));
        for(int score = 1; score < 4; ++score) 
            avg += scores[score];
        avg /= 3;
        if(avg > win_avg) {
            winner = name;
            win_avg = avg;
        }
        avg = 0;
    }
    std::cout << "Winner is: " << winner << "\n";
}


double validCheck(){
    double score;
    cin >> score;
    if (score < 1 || score > 10){
        cout << "Please Enter a score between 1 and 10: ";
        cin >> score;
    }
    return score;
}

If you want to find the average in a function and return the value you can do this

double calcAvgCheck(const double& scores[5]) {
    double avg = 0.0;
    for(int score = 1; score < 4; ++score) 
        avg += scores[score];
    avg /= 3;
    return avg;
}
ZachSand
  • 143
  • 8
  • Thank you, this has helped a lot, but one thing to be certain of, when trying to run this version (with a few modifications) "scores" and sort are undefined, what would you define them as? I've tried a few things with no success – Ace Ebert Apr 13 '15 at 01:20