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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/* This is a hot mess and i know it is! went out on a limb on many of these functions. */
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void PrintMenu();
void readin(string names[], int scores[], int &size);
float FindHighest(int scores[], int size);
float FindLowest(int scores[], int size);
float FindAverage(int scores[], int size);
void PrintRecords(string names[],int scores[], int size);
string FindID(string names[], int size, string stuID);
int main ()
{
// Print out the purpose of the program.
cout <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<< endl;
cout << endl;
cout <<"The purpose of this program is to read in grades from a file and give the ";
cout <<"user the option to print out the highest score in the file, lowest score in ";
cout <<"the file,the average score of the scores in the file, print scores of a "<< endl;
cout <<"spacific student, or print all of the scores."<< endl;
cout <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<< endl;
cout << endl;
string filename;
ifstream myIn;
char input;
string stuID;
// ask for user file input.
cout <<" Please enter the file to the read into the program."<< endl;
cin >> filename;
// convertring string name to C string type.
myIn.open ( filename.c_str() );
//test file
while (!myIn)
{
cout << endl;
cout <<" No!!!! Try Again"<< endl;
cin >> filename;
myIn.open ( filename.c_str() );
}
cout << endl;
// read values from the file into 2 arrays
readin (names, scores, size); // <------ names, scores, and size not declared in scope.
// repeat following until user say exit.
do {
// print menu
PrintMenu();
// read option
cout<<"Please Enter the number of the selection you wish to run. [1-3]."<<endl;
cin >> input;
cout<<endl<<endl;
// perform the option
// switch statement
// switch statment to determin action for user input.
switch (input)
{
case '1':
cout <<"The highest score is: " << FindHighest(scores, size) << endl;
break;
case '2':
cout <<"The lowest score is: " << FindLowest(scores,size) << endl;
break;
case '3':
cout <<"The average of the scores is: " << setprecision(2) << FindAverage(scores, size) << endl;
break;
case '4':
cout <<" Please enter the student ID."<<endl;
cin >> stuID;
cout <<"You entered "<< stuID << endl; cout<<endl <<endl;
cout <<"Student ID and score is: "<< FindID(names, scores, size);
break;
case '5':
PrintRecords(names, scores, size);
break;
case '6':
break;
default:
PrintMenu();
cout <<"PLease enter one of the selections 1, 2, 3"<< endl;
cin >> input;
break;
}
} while (input != 6);
}
void PrintMenu()
{
cout<<"============================ MENU =============================" << endl;
cout<<"| |"<< endl;
cout<<"| 1. Find the highest score. |"<< endl;
cout<<"| 2. Find the lowest score. |"<< endl;
cout<<"| 3. Find the average of the scores. |"<< endl;
cout<<"| 4. Find the student score for the student |"<< endl;
cout<<"| ID entered by the user. |"<< endl;
cout<<"| 5. Print Student Records. |"<< endl;
cout<<"| 6. Exit |"<< endl;
cout<<"| |"<< endl;
cout<<"==============================================================="<< endl;
}
void readin(string names[], int scores[], ifstream& myIn, int &size)
{
int i=0;
myIn >> names[i] >> scores[i];
while(myIn)
{
i++;
myIn >> names[i] >> scores[i];
}
size=i;
}
float FindHighest(int scores[], int size)
{
int temp=0;
for(int j=0; j<size; j++)
{
if(scores[j]>temp)
temp=scores[j];
}
return temp;
}
float FindLowest(int scores[], int size)
{
int temp=0;
for(int j=0; j<size; j++)
{
if(scores[j]<temp)
temp=scores[j];
}
return temp;
}
float FindAverage(int scores[], int size)
{
int sum=0;
float average=0;
int count=0;
for(int j=0; j<size; j++)
{
sum+=scores[j];
count++;
}
average=sum/count;
return average;
}
void PrintRecords(string names[],int scores, int size)
{
for(int j=0; j<size; j++)
{
cout << names[j] <<' '<< scores[j] << endl;
}
}
string FindID(string names[], int size, string stuID)
{
for(int i=0; i<size; i++)
{
if(names[i]==stuID)
return names[i];
}
}
|