Calculator for scoring dives in a competition I was looking for a light weight coding challenge and found this:


  In the sport of diving, seven judges award a score between 0 and 10, where each score may be a floating-point value. The highest and lowest scores are thrown out and the remaining scores are added together. The sum is then multiplied by the degree of difficulty for that dive. The degree of difficulty ranges from 1.2 to 3.8 points. The total is then multiplied by 0.6 to determine the diver’s score.
  Write a computer program that inputs a degree of difficulty and seven judges’ scores, and outputs the overall score for that dive. The program should ensure that all inputs are within the allowable data ranges.


So, I tried to write some modern c++, and I am looking for suggestions to modernize it or is it so basic that this isn't a concept.

#include "pch.h"
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

constexpr auto JUDGES = 7;
constexpr auto FUDGE_FACTOR = 0.6f;
constexpr auto MIN_DIFFICULTY = 1.2f;
constexpr auto MAX_DIFFICULTY = 3.8f;
constexpr auto MIN_SCORE = 0.f;
constexpr auto MAX_SCORE = 10.f;

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::stringstream;

float getInput(float low, float high)
{
    string line;
    float ret;    
    while (std::getline(cin, line))
    {
        stringstream ss(line);
        if (ss &gt;&gt; ret &amp;&amp; ss.eof()) {
            if (ret&gt;=low &amp;&amp; ret&lt;=high) return ret;
        }
        cout &lt;&lt; "invalid input, try again: ";
    }
    return -1.0;
}

int main()
{
    float difficulty;
    float total{ 0. };
    float min{ MAX_SCORE };
    float max{ MIN_SCORE };    

    cout &lt;&lt; "please enter degree of difficulty for the dive: ";
    difficulty = getInput(MIN_DIFFICULTY, MAX_DIFFICULTY);
    for (int i = 0; i &lt; JUDGES; ++i) {
        cout &lt;&lt; "please enter the judge #"&lt;&lt;i+1&lt;&lt;"'s score: ";
        auto score = getInput(MIN_SCORE, MAX_SCORE);
        if (score &lt; min) min = score;
        if (score &gt; max) max = score;
        total += score;
    }
    total -= (min + max);
    total *= difficulty * FUDGE_FACTOR;
    cout &lt;&lt; "The dive scored: " &lt;&lt; total &lt;&lt; endl;
}


Any thoughts/suggestions will be welcome.
Variables should be initialized with proper values when declared (if not, they should be declared as close to the point of usage as practical). This way there's no need to initialize them to some invalid value, or risk leaving them uninitialized. (difficulty and ret are the main offenders here, but total, min and max should be declared just before the loop using them).
I'm not sure there's much point in the using std::foo;. It's probably as much work to keep track of what you're using and update the declarations than just typing std:: where necessary. But it's better than using namespace std;, so that's good. :)
The prompt message to the user asking for input could be passed as a parameter to getInput and printed as part of the function.
The min and max values could also be shown when requesting an input.





Consider splitting the main function into two phases: getting input, calculating the result. This may make the program a little easier to understand.
Certain things could be split off into separate functions. e.g. String to float conversion and calculating the overall score.


These last two are really just suggestions though; the original code is very good.

Edited code, with some alternative ways of doing things:

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;cassert&gt;

constexpr auto JUDGES = 7;
constexpr auto FUDGE_FACTOR = 0.6f;
constexpr auto MIN_DIFFICULTY = 1.2f;
constexpr auto MAX_DIFFICULTY = 3.8f;
constexpr auto MIN_SCORE = 0.f;
constexpr auto MAX_SCORE = 10.f;

bool toFloat(std::string const&amp; string, float&amp; value)
{
    std::stringstream ss(string);
    return (ss &gt;&gt; value &amp;&amp; ss.eof());
}

bool isInRange(float value, float low, float high)
{
    return (value &gt;= low &amp;&amp; value &lt;= high);
}

float getFloatInput(float low, float high, std::string const&amp; prompt)
{
    while (true)
    {
        std::cout &lt;&lt; prompt &lt;&lt; " [" &lt;&lt; low &lt;&lt; ", " &lt;&lt; high &lt;&lt; "]: ";

        std::string line;
        std::getline(std::cin, line);

        auto value = 0.f;
        if (toFloat(line, value) &amp;&amp; isInRange(value, low, high))
            return value;

        std::cout &lt;&lt; "invalid input!\n";
    }

    assert(false);
    return -1.0f;
}

std::vector&lt;float&gt; getJudgesScores()
{
    auto scores = std::vector&lt;float&gt;();

    for (auto i = 0; i != JUDGES; ++i)
        scores.push_back(getFloatInput(MIN_SCORE, MAX_SCORE, "score #" + std::to_string(i + 1)));

    return scores;
}

float calculateOverallScore(std::vector&lt;float&gt; const&amp; scores, float difficulty)
{
    auto min = MAX_SCORE;
    auto max = MIN_SCORE;
    auto sum = 0.f;

    for (auto score : scores)
    {
        min = std::min(min, score);
        max = std::max(max, score);
        sum += score;
    }

    sum -= (min + max);

    return sum * difficulty * FUDGE_FACTOR;
}

int main()
{
    auto difficulty = getFloatInput(MIN_DIFFICULTY, MAX_DIFFICULTY, "difficulty");
    auto judgesScores = getJudgesScores();
    auto overallScore = calculateOverallScore(judgesScores, difficulty);

    std::cout &lt;&lt; "The dive scored: " &lt;&lt; overallScore &lt;&lt; std::endl;
}