1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include <iostream>
#include <fstream>
using namespace std;
//function prototypes
void displayHeading();
int displayMenu(/*out*/int);
void readNumbers(/*out*/double [],/*in*/ int);
void readFile(/*out*/double [],/*in*/ int);
void printAllScores(/*in*/const double[],/*in*/ int);
void printHighest(/*in*/ const double[],/*in*/ int);
void printLowest(/*in*/ const double[],/*in*/ int);
void printAverage(/*in*/ const double[],/*in*/ int);
void printOneNumber(/*in*/ const double[],/*in*/ int);
void evaluateChoice(/*in*/ int,/*in*/ double[],/*in*/ int);
void quitProgram();
int main(){
int choice;
const int numScores =10;
double userscore[numScores],
total,
lowesetScore,
average;
displayHeading();
displayMenu(choice);
while (choice !=8){
evaluateChoice(choice, userscore, numScores);
displayMenu(choice);
}
quitProgram();
return 0;
}
void displayHeading(){
cout <<" ---------------------------------------------------"<<endl;
cout <<"1-D ARRAY PROCESSING MENU OPTIONS\n";
cout <<"---------------------------------------------------\n";
}
int displayMenu(/*out*/ int choice)
{
cout <<"1. Read in 10 scores from user.\n";
cout <<"2. Read in 10 scores from the file, scores.txt.\n";
cout <<"3. Print all scores.\n";
cout <<"4. Print the highest score.\n";
cout <<"5. Print the lowest score.\n";
cout <<"6. Print the average score.\n";
cout <<"7. Print one score (give its entry number)\n";
cout <<"8. Quit program\n";
cin >> choice;
return choice;
}
void readNumbers(/*out*/double userscore[],/*in*/ const int numScores){
int count;
for ( count =0; count < numScores-1; count++) {
cout <<"Enter score #" <<(count +1) << ": ";
cin >>userscore[count];
}
}
void evaluateChoice(/*in*/ int choice,/*in*/ double userscore[],/*in*/ const int numScores){
switch (choice) {
case 1:
cout <<"testing\n";
readNumbers(userscore,numScores);
break;
case 2:
readFile(userscore, numScores);
break;
case 3:
printAllScores(userscore,numScores);
break;
case 4:
printHighest(userscore, numScores);
break;
case 5:
printLowest(userscore, numScores);
break;
case 6:
printAverage(userscore, numScores);
break;
case 7:
printOneNumber(userscore, numScores);
break;
default : cout << "Invalid entry. Please pick an appropriate menu number.\n";
}
}
void readFile(/*out*/ double userscore[],/*in*/ const int numScores){
string filename;
ifstream inFile;
cout <<"Please enter filename: ";
cin >>filename;
if (filename == "scores.txt"){
inFile.open(filename.c_str());
for(int count = 0; count < numScores; count++)
inFile>> userscore[count];
cout <<"File has successfully opened and 10 scores have been read.\n";
cout <<"Please select the print all scores menu option to view the scores.\n";
if (!inFile)
{
cout << "File error." << endl;
}
}
else
cout <<"File not found. Please retype the file." << endl;
cin >>filename;
inFile.close();
}
void printAllScores(/*in*/const double array1[], /*in*/const int numScores){
int count;
cout<<"Here are your scores:"<<endl;
for (count =0; count <numScores-1; count++){
cout <<" Score #"<<count +1;
cout << ": " <<array1[count]<<endl;
}
}
void quitProgram(){
cout <<"Now exiting the program."<<endl;
exit(0);
}
void printHighest(/*in*/ double userscore[],/*in*/ const int numScores){
int highest;
highest = userscore[9];
for (int i = 0; i < numScores; i++) {
if (highest > userscore[i]){
highest = userscore[i];
}
}
cout <<"The highest score is "<<highest<<endl;
}
void printLowest(/*in*/ double userscore[],/*in*/ const int numScores){
int lowest = userscore[9];
for (int i = 0; i < numScores; i++) {
if (lowest < userscore[i]){
lowest = userscore[i];
}
}
cout <<"The lowest score is "<<lowest<<endl;
}
void printAverage(/*in*/double userscore[],/*in*/ const int numScores){
int sum = 0;
for (int count = 0; count < numScores; count++) {
sum = sum + userscore[count];
}
int average = sum / numScores;
cout <<"The average score is "<<average<<endl;
}
void printOneNumber(/*in*/double userscore[],/*in*/ const int numScores){
int userRow;
cout <<"Please enter entry or row # of score you want: \n";
cin >>userRow;
cout <<"Entry #"<<userRow<<" ";
cout <<"Score: "<<userscore[userRow]<<endl;
cout <<"Score statistics: ";
}[code][code]
|