Topic: Files and Streams
Codes discussed will be posted here. Write your conclusion
Filename: "Ch5_stData.txt"
Given problem:
Steve Gill 89
Rita Johnson 91.5
Randy Brown 85.5
Seema Arora 76.5
Samir Mann 73
Samantha McCoy 88.5
*Note: Need the proper codes
*Note need proper codes and conclusion on how its done and about the results. And please give the proper solution and explanation.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string firstName;
string lastName;
double testScore;
char grade = ' ';
double sum = 0;
int count = 0;
ifstream inFile;
ofstream outFile;
inFile.open("Ch5_stData.txt");
if (!inFile) {
cout << "Cannot open input file. "
<< "Program terminates!" << endl;
return 1;
}
outFile.open("Ch5_stData.out");
outFile << fixed << showpoint << setprecision(2);
inFile >> firstName >> lastName;
inFile >> testScore;
while (inFile)
{
sum = sum + testScore;
count++;
switch (static_cast<int> (testScore) / 10) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
grade = 'F';
break;
case 6:
grade = 'D';
break;
case 7:
grade = 'C';
break;
case 8:
grade = 'B';
break;
case 9:
case 10:
grade = 'B';
break;
default:
cout << "Invalid score." << endl;
}
outFile << left << setw(12) << firstName
<< setw(12) << lastName
<< right << setw(4) << testScore
<< setw(2) << grade << endl;
inFile >> firstName >> lastName;
inFile >> testScore;
}
outFile << endl;
if (count != 0)
outFile << "Class Average: " << sum / count << endl;
else
outFile << "No data." << endl;
inFile.close();
outFile.close();
return 0;
}
Comments
Leave a comment