C++才艺竞赛评分程序非数值输入问题的解决咨询
Fixing Non-Numeric Input Validation in Your Judge Score Program
Hey there! Let's resolve the issue where your program accepts non-numeric input. The core problem in your current getJudgeData function is that after handling invalid input, the loop still checks the score range even when score holds an undefined value (from the failed input). Here's how to fix it properly:
Modified getJudgeData Function
void getJudgeData(string jname, double &score) { bool validInput = false; do { cout << "Data entry for " << jname << ". Score must be in the range 0 - 10: "; // First, check if the input is a valid double if (cin >> score) { // Now validate the range if (score >= 0 && score <= 10) { validInput = true; } else { cout << "Invalid. Score must be between 0 and 10. Please re-enter score." << endl; } } else { // Handle non-numeric input cout << "Invalid. Input must be numeric. Please re-enter score." << endl; cin.clear(); // Reset the error state of cin // Ignore all remaining characters in the input buffer until newline cin.ignore(numeric_limits<streamsize>::max(), '\n'); } } while (!validInput); }
Key Improvements Explained
- Separate Validation Steps: We first confirm the input is a valid numeric value before checking its range. This avoids checking an undefined
scorevalue when input is non-numeric. - Valid Input Flag: Using a
validInputboolean makes the loop logic clearer—we only exit when both the input is numeric and within the correct range. - Utilize the
jnameParameter: The original code didn't use the judge name parameter, so we added it to the prompt to make user input more intuitive. - Robust Buffer Clearing: After clearing the error state, we ensure the entire invalid input line is discarded, preventing leftover characters from causing issues in subsequent inputs.
Bonus: Simplify Highest/Lowest Functions
Your current findHighest and findLowest functions work, but they can be simplified using standard library functions to make the code cleaner:
#include <algorithm> // Add this header at the top double findLowest(double score1, double score2, double score3, double score4, double score5) { return min({score1, score2, score3, score4, score5}); } double findHighest(double score1, double score2, double score3, double score4, double score5) { return max({score1, score2, score3, score4, score5}); }
This uses C++11 initializer lists with std::min and std::max from the <algorithm> header, which is more concise and less error-prone than manual comparisons.
Full Modified Code
#include <iostream> #include <iomanip> #include <limits> #include <algorithm> // Added for min/max functions #include <string> // Ensure string is included explicitly using namespace std; //Function prototypes void getJudgeData(string jname, double &score); double getAverage(double score1, double score2, double score3, double score4, double score5); double findLowest(double score1, double score2, double score3, double score4, double score5); double findHighest(double score1, double score2, double score3, double score4, double score5); int main() { //Variables double score1, score2, score3, score4, score5, average, lowest, highest; //Get input from five judges std::cout << "Enter the scores for the five judges." << endl; getJudgeData("Judge 1", score1); getJudgeData("Judge 2", score2); getJudgeData("Judge 3", score3); getJudgeData("Judge 4", score4); getJudgeData("Judge 5", score5); average = getAverage(score1, score2, score3, score4, score5); lowest = findLowest(score1, score2, score3, score4, score5); highest = findHighest(score1, score2, score3, score4, score5); system("pause"); return 0; } /* getJudgeData ** Function should ask the user for a judge's score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the five judges. */ void getJudgeData(string jname, double &score) { bool validInput = false; do { cout << "Data entry for " << jname << ". Score must be in the range 0 - 10: "; if (cin >> score) { if (score >= 0 && score <= 10) { validInput = true; } else { cout << "Invalid. Score must be between 0 and 10. Please re-enter score." << endl; } } else { cout << "Invalid. Input must be numeric. Please re-enter score." << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } } while (!validInput); } /* calcScore Function should calculate and return the average of the three scores that remain after dropping the highest and lowest scores. */ double getAverage(double score1, double score2, double score3, double score4, double score5) { double lowest, highest, average; //Find lowest lowest = findLowest(score1, score2, score3, score4, score5); //Find highest highest = findHighest(score1, score2, score3, score4, score5); //Calculate average average = (score1 + score2 + score3 + score4 + score5 - highest - lowest) / 3; cout << setprecision(3) << "Average: " << average << endl; cout << setprecision(3) << "Lowest: " << lowest << endl; cout << setprecision(3) << "Highest: " << highest << endl; return average; } //findLowest double findLowest(double score1, double score2, double score3, double score4, double score5) { return min({score1, score2, score3, score4, score5}); } //findHighest double findHighest(double score1, double score2, double score3, double score4, double score5) { return max({score1, score2, score3, score4, score5}); }
内容的提问来源于stack exchange,提问作者Connor O'Brien
相关产品推荐
相关产品推荐

