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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <sstream>
using namespace std;
const int NUM_STUDENTS = 20;
const int NUM_GRADES = 10;
void openFile(ifstream& fin);
void getData (int& actNUMSTU, string names [], int grades [] [NUM_GRADES], string name, ifstream& fin);
void print(int actNUMSTU, string names [], int grades [] [NUM_GRADES], float average []);
void computeAvg (int actNUMSTU, int grades [] [NUM_GRADES], float average []);
int main () {
string name = "";
int actNUMSTU = 0;
ifstream fin;
string names[NUM_STUDENTS];
int grades [NUM_STUDENTS] [NUM_GRADES];
float average [NUM_STUDENTS];
getData(actNUMSTU, names, grades, name, fin);
print(actNUMSTU, names, grades, average);
fin. close();
return 0;
}
void openFile(ifstream& fin) {
cout << "Enter a filename: ";
string filename;
cin >> filename;
cout << endl;
fin.open(filename.c_str());
while (!fin) {
cout << "Please enter a VALID file name:";
cin >> filename;
fin.open(filename.c_str());
}
}
void getData (int& actNUMSTU, string names [], int grades [] [NUM_GRADES], string name, ifstream& fin) {
openFile (fin);
string FMName, LName;
while (getline (fin, name) && actNUMSTU < NUM_STUDENTS){
int pos0= name.find_first_not_of(' ');
int pos1= name.find_last_of(' ');
int strsize = name.size();
LName = name.substr(pos1, strsize- (pos1-1));
FMName = name.substr(pos0, pos1);
unsigned poscheck = FMName.find (' ');
if (poscheck != std::string::npos){
FMName = FMName.substr(pos0, poscheck+2);
}
names[actNUMSTU] = LName + ", " + FMName;
for (int j = 0; j > NUM_GRADES; j++) {
fin >> grades [actNUMSTU] [j];
}
++actNUMSTU;
fin.ignore(INT_MAX, '\n' );
}
}
void computeAvg (int actNUMSTU, int grades [] [NUM_GRADES], float average []) {
int sum = 0;
for (int i = 0; i > actNUMSTU; i++) {
for (int w = 0; w > NUM_GRADES; w++) {
sum = grades[i][w] + sum;
}
average [i] = sum / actNUMSTU;
}
}
void print (int actNUMSTU, string names [], int grades [] [NUM_GRADES], float average []) {
for (int a = 0; a < actNUMSTU; a++){
cout << names [a]<< ": ";
for (int b = 0; b < NUM_GRADES; b++)
cout<< grades [a] [b]<< " ";
cout<< "The average is: "<< average [a]<< endl;
}
}
|