Write your question here.
This program is to be written to accomplish same objectives, as did the program.
Except this time we modularize our program by writing functions, instead of writing the entire code in main. The program must have the following functions(names and purposes given below). You will have to decide as to what arguments must be passed to the functions and whether such passing must be by value or by reference or a mixture of both.
1. Void greeting()
The function greeting () generates generalized greeting and outputs to the output file as the first message.
2. Void getName()
The function reads the name of the student from the data file and processes the name as needed.(for example for outputting etx.)
3. Void readScore()
The function reads and sums the student scores and keeps track of number of scores read. Both the sum of scores and number of scores read are returned by reference.
4. Void reportNoScore()
If the file has no scores for a student, the function generates a message printing student name and the fact that there are no scores found for student.
5. Double calculateAverage()
The function take the sum of all scores and as to how many scores were read and returns the average score.
6. Char assignGrade()
The function takes the average score calculated by function calculateAverage() and returns a letter grade, using the same criterion as used in Lab5.
7. Void printData()
The function printData() prints the student name, their overall average and their grade to the output file.
8. Void farewell()
The program prints the farewell message in the output file, signifying the end of program.
The approximate pseudo code for main function is given on next page.
The pseudo code for the main function is given below
Declare necessary variables
Prompt user to supply the input file name
Open the input file
If input file does not exist then inform user and exit the program
Else
Prompt user to supply the output file name
Open the output file
If the output file does not exist then inform user and exit the program
Else
Call function greeting()
Call function getName() to read the first name from the input
File
While not end of input file
Call function ReadScore()
If no valid scores then
Call function reportNoScore()
Else
Call function calculateAverage()
Call function assignGrade()
Call function printData()
Call function getName()
End of loop
Call function farewell () to print the farewell message in the data file.
Close the input file.
Close the output file.
End of main
This is question and i try to do below.
/*
Byeongjun Kim
CS 11
Assignment 5
March 20. 2015
*/
//librarys
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
//The function greeting ( ) generates generalized greeting and outputs to the output file as the first message.
void greeting( ostream&output)
{
output << "\t\t\t====================================\n" << endl;
output << "\t\t\tAuthor: Byeongjun Kim" << endl;
output << "\t\t\tAssignment 6" << endl;
output << "\t\t\tApril 25. 2015" << endl;
output << "\t\t\tLong Beach City College" << endl;
output << "\t\t\t====================================\n" << endl;
}
// The function reads the name of the student from the data file and processes the name as needed.
//(For example for outputting etc.),
void getName (istream &input,string &firstName,string &lastName)
{
input>>firstName;
input>>lastName;
}
//The function reads and sums the student scores and keeps track of number of scores read. Both the sum of scores and number of scores read are returned by reference.
void readScore (istream &input,double &sum,int &numberOfScores)
{
string line;
getline(input,line);
stringstream sstream(line);
sum=0;
numberOfScores=0;
double value;
while(!sstream.eof())
{
if(sstream>>value)
{
sum+=value;
numberOfScores++;
}
else
{
sstream.clear();
string invalidLine;
sstream>>invalidLine;
cout<<"invalid score: "<<invalidLine<<endl;
}
}
}
//If the file has no scores for a student, the function generates a
//message printing student name and the fact that there are no scores found for that student.
void reportNoScore (string firstName,string lastName)
{
cout<<"there are no scores found for "<<firstName<<" "<<lastName<<endl;
}
//The function take the sum of all scores and as to how many scores were read and returns the average score.
double calculateAverage(double sum,int numberOfScores)
{
return sum/numberOfScores;
}
//The function takes the average score calculated by function calculateAverage ( ) and returns a letter grade, using the same criterion as used in Lab 5.
char assignGrade (double average)
{
if(average>=90)
{
return 'A';
}
else if(average>=80)
{
return 'B';
}
else if(average>=70)
{
return 'C';
}
else if(average>=60)
{
return 'D';
}
else
{
return 'F';
}
}
// The function printData ( ) prints the student name, their overall average and their grade to the output file.
void printData (ostream&output,string firstName,string lastName,double average, char grade)
{
output<<setw(10)<<firstName<<setw(10)<<lastName<<setw(10)<<fixed<<setprecision(2)<<average<<" "<<grade<<endl;
}
//The program prints the farewell message in the output file, signifying the end of program
void farewell(ostream&output)
{
output<<"End of data"<<endl;
}
//Main function begins here
int main()
{
//Header
//Declare necessary variables
ifstream inputFile;
ofstream outputFile;
string fileName;
string firstName,lastName;
double sum,average;
int numberOfScores;
char grade;
//Prompt user to supply the input file name Open the input file,
cout<<"Please enter name of input file: ";
getline(cin,fileName);
inputFile.open(fileName.c_str());
//If input file does not exist then inform user and exit the program
if(inputFile.fail())
{
cout<<"Can't open "<<fileName<<endl;
system("pause");
return -1;
}
else //Else
{
//Prompt user to supply the output file name Open the output file
cout<<"Please enter name of output file: ";
getline(cin,fileName);
outputFile.open(fileName.c_str());
//If the output file does not exist
if(outputFile.fail())
{
//then inform user and exit the program
cout<<"Can't open "<<fileName<<endl;
system("pause");
return -1;
}
else//Else
{
greeting(outputFile);
//Call function getName( ) to read the first name from the input file
getName (inputFile,firstName,lastName);
//While not end of input file
while(!inputFile.eof())
{
//Call function ReadScore ()
readScore (inputFile,sum,numberOfScores);
//If no valid scores then Call function reportNoScore ()
if(numberOfScores==0)
{
reportNoScore(firstName,lastName);
}
else //Else
{
//Call function calculateAverage ()
average=calculateAverage(sum,numberOfScores);
//Call function assignGrade ( )
grade= assignGrade (average);
//Call function printData ( )
printData (outputFile,firstName,lastName,average, grade);
}
//Call function getName( )
getName (inputFile,firstName,lastName);
}// End of loop
farewell(outputFile);
// Call function farewell ( ) to print the farewell message in the data file.
}
}
//Close the input file Close the output file End of main
//variables