In: Computer Science
#include <iostream>
#include <fstream>
using namespace std;
int main(){
int nscores;
cout << "Number of Test scores : ";
cin >> nscores;
string fnames[nscores];
float scores[nscores];
int i=0;
// Reading first names and test scores
while( i < nscores ) {
cout << "Enter First Name : ";
cin >> fnames[i];
cout << "Enter Score : ";
cin >> scores[i];
i++;
}
i = 0;
// printing names and test scores
cout << "--------------------------------"<<endl;
cout << "Name"<<"\t"<<"Score" << endl;
while( i < nscores ) {
cout << fnames[i] <<"\t"<< scores[i] <<endl;
i++;
}
cout << "--------------------------------"<<endl;
int j = 0;
ofstream fs;
//clearing file with empty text
fs.open("midTermScores.txt");
fs << "";
fs.close();
fs.open("midTermScores.txt", ios::app);
// writing first name and scores on file
while( j < nscores ) {
fs << fnames[j] <<"\t"<< scores[j] <<endl;
j++;
}
// closing file
fs.close();
int k = 0;
// printing firstnames and grades
cout << "Name"<<"\t"<<"Grade" << endl;
while( k < nscores ) {
int n = scores[k];
char grade;
if( n >= 90 && n <= 100 ){
grade = 'A';
}else if(n >= 80 && n < 90){
grade = 'B';
}else if(n >= 70 && n < 80){
grade = 'C';
}else if(n >= 60 && n < 70){
grade = 'D';
}else{
grade = 'F';
}
cout << fnames[k] << "\t" << grade <<endl;
k++;
}
return 1;
}
Output: