Question

c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did...

c++ CSI Lab 6 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). Fot o tentoutefill datere nedefremfite 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 file as the first message. generates generalized greeting and outputs to the output 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 etc.), 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 that student. 5. double calculate Average () 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 calculate Average () and returns a letter grade, using the same criterion as used in Lab 5. 7. void printData() The function print Data grade to the output file. prints the student name, their overall average and their 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 calculate Average () Call function assignGrade () Call function printData() Call function getName() End of loop to print the farewell message in the data file. Call function farewell Close the input file Close the output file End of main .cpp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

_________________

dataInput.txt (Input file)

Mickey Mouse 67 78 89
Minnie Mouse 78 76 65
Jack Robinson
Jimmy Johnson 89 98 78 76 77

_____________________

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;

// Function Declarations
void greeting(ofstream& dataOut);
void getName(ifstream& dataIn,string &name);
void readScore(ifstream& dataIn,int &sum,int &cnt);
void reportNoScore(string name);
double calculateAverage(int sum,int cnt);
char assignGrade(double avg);
void printData(ofstream &dataOut,string name,double avg,char gradeLetter);
void farewell(ofstream &dataOut);
int main() {
//Declaring variables
ifstream dataIn;
ofstream dataOut;
int sum=0,cnt=0;
string fname,lname,name;
  
//Opening the input file
dataIn.open("dataInput.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
//Opening the output file
dataOut.open("dataOutFile3.txt");
  
//setting the precision to two decimal places
dataOut << std::setprecision(2) << std::fixed;
//Reading the data from the file
greeting(dataOut);
  
if (!dataIn.fail()) {
//Reading the data from the file
getName(dataIn,name);
}
while(!dataIn.fail())
{
sum=0;
cnt=0;
  
readScore(dataIn,sum,cnt);
  
if(cnt==0)
{
reportNoScore(name);
}
else
{
   double avg=calculateAverage(sum,cnt);
  
char gradeLetter=assignGrade(avg);

printData(dataOut,name,avg,gradeLetter);

  
}   
getName(dataIn,name);
}

dataIn.close();
farewell(dataOut);
}
return 0;
}
void greeting(ofstream& dataOut)
{
dataOut<<"** Welcome to the Program which calculates students average **"<<endl;
}
void getName(ifstream& dataIn,string &name)
{
string fname,lname;
dataIn>>fname>>lname;
name=fname+" "+lname;
}
void readScore(ifstream& dataIn,int &sum,int &cnt)
{
string line;
string token;
getline(dataIn,line);
stringstream ss(line);
while(getline(ss, token, ' '))
{
sum+=atoi(token.c_str());
cnt++;
}
}
void reportNoScore(string name)
{
cout<<"No score for "<<name<<endl;
}
double calculateAverage(int sum,int cnt)
{
return ((double)sum)/cnt;
}
char assignGrade(double average)
{
   char gradeLetter;
   if (average >= 90 && average<=100)
gradeLetter = 'A';
else if (average >= 80 && average < 90)
gradeLetter = 'B';
else if (average >= 70 && average < 80)
gradeLetter = 'C';
else if (average >= 60 && average < 70)
gradeLetter = 'D';
else if (average < 60)
gradeLetter = 'F';

return gradeLetter;
}
void printData(ofstream &dataOut,string name,double avg,char gradeLetter)
{
   dataOut<<"Name :"<<name<<endl;
   dataOut<<"Average :"<<avg<<endl;
   dataOut<<"Grade Letter :"<<gradeLetter<<endl;
     
}
void farewell(ofstream &dataOut)
{
   dataOut<<"End of the Program.Thank you!"<<endl;
   dataOut.close();
}

______________________________

Output:

_________________________________

// dataOutFile3.txt (Output file)

_____________________Thank You

Add a comment
Know the answer?
Add Answer to:
c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes...

    CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes the user and displays the purpose of the program. It then prompts the user to enter the name of an input file (such as scores.txt). Assume the file contains the scores of the final exams; each score is preceded by a 5 characters student id. Create the input file: copy and paste the following data into a new text file named scores.txt DH232 89...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions             ...

    Lab 7: Void and Value-Returning Functions Lab 7: Void and Value-returning functions              Due date: 11/6/19 Problem: Write a C++ program that calculates the areas of a rectangle and of an ellipse.                    Area = base * height             Area = π * radius a * radius b Note: all images extracted from http://www.mathsisfun.com/area-calculation-tool.html ------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below. ------------------------------------------------------------------------------------------------------------------------ Part A (79...

  • unctions with No Parameters Summary In this lab, you complete a partially prewritten Python program that...

    unctions with No Parameters Summary In this lab, you complete a partially prewritten Python program that includes a function with no parameters. The program asks the user if they have preregistered for art show tickets. If the user has preregistered, the program should call a function named discount() that displays the message "You are preregistered and qualify for a 5% discount." If the user has not preregistered, the program should call a function named noDiscount() that displays the message "Sorry,...

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT