0

So, I am writing code to take in 4 scores each from 3 archers. I am doing this with for loops and have to add validation in the form of a cin.fail. So far i am able to have it recognize that the input is incorrect the first time i enter something but if i try and enter something incorrect again it moves on to the next score.

ex. Please enter the first score for archer 1: (anything besides a whole number is entered)

Entry must be numeric!

Please enter the first score for archer 1: (anything besides a whole number is entered)

Please enter the 2nd score for archer 1:

Here is my code any help would be appreciated.

for (archernum = 1; archernum <= MAXARCHERS; archernum++)
{
    total = 0;
    for (endnum = 1; endnum <= MAXENDS; endnum++)
    {
        cout << "Please enter score recorded in end " << endnum << " for archer number " << archernum << ": ";
        cin >> score;
        if (cin.fail())
        {
        cin.clear();
        cin.ignore (1000, '\n');

        cout << "\nThe score must be entered as a number.";
        cout << "\nPlease enter score recorded in end " << endnum << " for archer number " << archernum << ": ";
        cin >> score;
        }
        total += score;
    }
    cout <<"\nThe total score for archer number "<< archernum << " is: " <<  total << "\n\n";
}
average = total / MAXENDS;
cout << "\nThe average score for the archers is: " << average;
cout << endl << endl;
Brett
  • 43
  • 1
  • 7

0 Answers0