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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <string>
using namespace std;
void readfile(string& f_key, string f_first[], string f_last[], string f_grades[], string inputfilename);
void printstudents(string& f_key, string f_first[], string f_last[], int f_avg[]);
void outputgrades(string& f_key, string f_first[], string f_last[], int f_avg[], string outputfilename);
void gradestudents(string& f_key, string f_grades[], int f_avg[]);
int main()
{
string first[50];
string last[50];
string grades[50];
int avg[50];
string key;
string inputfilename;
string outputfilename;
ifstream inf;
ofstream outf;
cout << "Enter name of input file: " << endl;
cin >> inputfilename;
cout << "Enter name of output file: " << endl;
cin >> outputfilename;
readfile(key, first, last, grades, inputfilename);
gradestudents(key, grades, avg);
printstudents(key, first, last, avg);
outputgrades(key, first, last, avg, outputfilename);
outf.close();
return 0;
}
void readfile(string& f_key, string f_first[], string f_last[], string f_grades[], string inputfilename)
{
ifstream inf;
int studentiterator = 0;
inf.open(inputfilename.c_str());
inf >> f_key;
while (!inf.eof())
{
inf >> f_first[studentiterator];
inf >> f_last[studentiterator];
inf >> f_grades[studentiterator];
studentiterator++;
}
inf.close();
}
void printstudents(string& f_key, string f_first[], string f_last[], int f_avg[])
{
ofstream outf;
outf << f_key << endl;
for (int i = 0; i < 5; i++)
{
outf << f_first[i] << " " << f_last[i] << " " << f_avg[i] << endl;
}
}
void outputgrades(string& f_key, string f_first[], string f_last[], int f_avg[], string outputfilename)
{
ofstream outf;
outf.open(outputfilename.c_str());
outf << left << setw(10) << "NAMES" << right << setw(25) << "SCORES" << endl;
for (int i = 0; i < 5; i++)
{
outf << f_last[i] << ", " << f_first[i] << " " << f_avg[i] << endl;
}
int students = 0;
for (int i = 0; i < 50; i++)
{
if (f_first[i] != "")
{
students++;
}
}
outf << "# of students: " << students << endl;
double dubtemp = 0.0;
students = 0;
for (int i = 0; i < 50; i++)
{
if (f_avg[i] != 0)
{
dubtemp = dubtemp + f_avg[i];
students++;
}
}
dubtemp = dubtemp / students;
outf << "Average: " << dubtemp << endl;
}
void gradestudents(string& f_key, string f_grades[], int f_avg[])
{
for (int i = 0; i < 50; i++)
{
int num = 0;
for (int j = 0; j < f_grades[i].length(); j++)
{
char t1, t2;
t1 = tolower(f_key[j]);
t2 = tolower(f_grades[i][j]);
if (t1 == t2)
{
num += 5;
}
}
f_avg[i] = num;
}
}
|