/* Scores.cpp Dave Klick CIS 150 2017-03-28 Demo of arrays. */ #include #include #include #include using namespace std; int getInt(string prompt, int min, int max); void getScores(int ar[], int len); void display(int ar[], int len); int getTotal(int ar[], int len); int min(int ar[], int len); int max(int ar[], int len); double average(int ar[], int len); void writeToFile(string fname, int ar[], int len); void readFile(string fname); int main() { const int SIZE = 5; int score[SIZE]; int sum; string filename("elizabeth.txt"); getScores(score, SIZE); display(score, SIZE); sum = getTotal(score, SIZE); cout << "The sum is " << sum << endl; cout << "The highest score is " << max(score, SIZE) << endl; cout << "The lowest score is " << min(score, SIZE) << endl; cout << "The average score is " << average(score, SIZE) << endl; writeToFile(filename, score, SIZE); readFile(filename); return 0; } void writeToFile(string fname, int ar[], int len) { ofstream ofile(fname.c_str()); if (!ofile) { cout << "Error opening file " << fname << " for output\n"; exit(EXIT_FAILURE); } for (int i=0; i> num) { cout << num << " "; } cout << endl; ifile.close(); } void getScores(int ar[], int len) { for (int i=0; i largest) largest = ar[i]; } return largest; } double average(int ar[], int len) { return (double) getTotal(ar, len) / len; } int getInt(string prompt, int min, int max) { int val; bool valid = false; while (!valid) { // Keep looping while we do not have valid input cout << prompt << "(" << min << "-" << max << "): "; cin >> val; if (!cin) { // This is true if the wrong data type was entered cout << "Invalid integer\n"; cin.clear(); // Clears the input error condition cin.ignore(10000, '\n'); // Clears the input buffer } else { // The rest of this is normal data validation if (val < min) cout << "Error: Below minimum of " << min << '\n'; else if (val > max) cout << "Error: Above maximum of " << max << '\n'; else valid = true; // Input was valid, so set flag to stop loop } } return val; }