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
|
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
//********************PROTO****************************8
void greeting(ofstream& out);
void getName(ifstream& in, string& firstName, string& lastName);
void readData(ifstream& in, double& sum, int& count);
void reportNoScore(ifstream& in, string& firstName, string& lastName);
double calculateAverage(double& sum, int& count);
void printData(string&, string&, double&, char);
char assignGrade(double);
//******************** MAIN FUNCTION///////////////////////////
int main()
{
string inputFile = "";
string outputFile = "";
string line;
string fName = "";
string lName = "";
double sum = double();
int count = int();
char grade = ' ';
double avg = 0.00;
cout << "What is the input file name? " << endl;
cin >> inputFile;
inputFile += ".txt";
ifstream in;
in.open(inputFile.c_str());
if (!in) //if in file does not exist do this
{
cout << "The input file does not exist, this program wil end.";
}
else
{
cout << "What will output file be name? ";
cin >> outputFile;
outputFile += ".txt";
ofstream out;
out.open(outputFile.c_str());
if (!out)
{
cout << "The output file does not exist, this program wil end.";
}
else
{
greeting(out);
while (!in.eof())
{
getName(in, fName, lName);
readData(in, sum, count);
if (sum == 0)
{
reportNoScore(in, fName, lName);
}
avg = calculateAverage(sum, count);
grade = assignGrade(avg);
printData(fName, lName, avg, grade);
}
}
}
system("PAUSE");
}
//*****************************MODULES********************************************************
void greeting(ofstream& out)
{
cout << "Hello, this program will calculate the average and grade a student from file." << endl;
out << "Hello, this program will calculate the average and grade a student from file." << endl;
}
void getName(ifstream& in , string& firstName, string& lastName)
{
in >> firstName >> lastName;
while (in.fail())
{
in.clear();
in.ignore(1, '\n');
in >> firstName >> lastName;
}
}
void readData(ifstream& in, double& sum, int& count)
{
double tempSum = double();
sum = 0.0;
count = 0;
while ((in.peek() != '\n') && (!in.eof())) //.peek() looks at next character without using it, so this checks for end of line
{
in >> tempSum;
while (in.fail())
{
in.clear();
in.ignore(1, '\n');
in >> tempSum;
}
sum = sum + tempSum;
++count;
}
}
void reportNoScore(ifstream& in, string& firstName, string& lastName)
{
cout << "No score found for " << firstName << " " << lastName << endl;
}
double calculateAverage(double& sum, int& count)
{
return sum / static_cast<int>(count);
}
char assignGrade(double avg)
{
if (avg >= 90)
return 'A';
else if (avg >= 80)
return 'B';
else if (avg >= 70)
return 'C';
else if (avg >= 60)
return 'D';
else if (avg < 60)
return 'F';
}
void printData(string& firstName, string& lastName, double& Avg, char grade)
{
cout << firstName << " " << lastName << " " << Avg << " " << grade << endl;
}
|