#include<iostream> using namespace std; const int ARSIZE = 10; void show_mark(float s) { cout << "Your average score is: "<< s << endl; } float avg(float arg[], int n) { float sum = 0; for (int i = 0; i < n; i++) sum += arg[i]; return sum/n; } int main() { float mark[ARSIZE]; float score; float total = 0; for (int i = 0; i < ARSIZE; i++) { cout <<"Please enter #" << i + 1 << " mark: "<< endl; cin >> score; if(!cin) { cin.clear(); if(cin.get() != '\n') continue; cout << "Bad input! Please enter your mark correctly." << endl; break; } else if(mark[i] < 0) break; mark[i] = score; } total = avg(mark, ARSIZE); show_mark(total); return 0; }
只是自己练个熟练度,大神们不要点开,免得瞎眼睛。。。