check your input

#include <iostream>
using namespace std;

float avg_score(float sc, int num)
{
    return sc/num;
}

void show_score(float avg)
{
    cout << "Your average score is " << avg << endl;
}

float input_score(float sc[], int num)
{
    float total;
    total = 0;
    for(int i = 0; i < num; i++)
    {
        cout << "# Round " << i+1 << "# score: ";
        while(!(cin >> sc[i]))
        {
            cout << "Bad input! Please enter your score!"<<endl;
            cin.clear();
            while(cin.get() != '\n')
                continue;
        }
        total = total + sc[i];
    }
    return total;
}
int main()
{
    float result,average;
    int LEN;
    cout << "Please enter your round : " << endl;
    cin >> LEN;
    float mark[LEN];
    result = input_score(mark, LEN);
    average = avg_score(result, LEN);
    show_score(average);

    return 0;
}
get your average score
原文地址:https://www.cnblogs.com/TadGuo/p/7609248.html