Programming Assignment 1 Functions, arrays of structures, arrays, file input, file output, console output, output format
Posted: Tue Jul 12, 2022 8:03 am
Programming Assignment 1
Functions, arrays of structures, arrays, file input, fileoutput, console output, output formatting, header file
Program description:
The input file contains the grade keyas the first row of data consisting of true and false answers,i.e., T and F:
TTFTFTTTFTFTFFTTFTTF
The remaining data in the filecontains a student’s first name, last name and answer to thetest. For example the second row of the filecontains:
Trent Watt T FTFTFTTTFTTFTTF TF
Read the first row into a variable,type of your choice, as the key to the correct answers. Output the key data to the console (cout).
Read the remaining file data andcompute the score by comparing the student answers to the gradekey. Each correct answer is 5 points. A space in thestudent answer set indicates the student did not answer thequestion. Read until end of file.
First Name
Last Name
Test Answers
Numerical Score
Letter Grade
Check the status of the file openfunctions. If the file does not open, display a message andexit the program.
The test has 20 questions. Eachcorrect answer is awarded five (5) points, each wrong answer orunanswered question gets zero (0) points.
Assume the following grade scale:
A: 90%-100%
B: 80%-89.99%
C: 70%-79.99%
D: 60%-69.99%
F: 0-59:99%
<A title of your choosing>
Name Score Letter Grade Student Answers
Ensure each line of data is neat andreadable. All score values must have two decimalplaces. Use manipulators such as setw, right and left to aidin neatess and readability..
Call the function from main. Declare the prototype in the header file. Code the functionheader and body after main.
Basic Comments:
Program Name: (name of programfile)
Author: (your name)
Brief explanation of programpurpose
Pre/Postconditions:
//Precondition: what must betrue about any parameters prior to function call. There maybe no preconditions
//Postconditions: what is trueafter function is complete
See pre/post condition comment examplein provided example program Documentation.cpp in ProgrammingAssignment 1 link.
Test and evaluate calculations for accuracy. Points will be taken for inaccurate calculations, improperformatting, directions not followed.
Turn in file code *.cpp; README file; header file *.h , inputfile and output file. You DO NOT need to submit theexecutable (*.exe). You MAY zip all the files and submit ifyou choose. You MUST include all the files indicated orpoints will be deducted.
README must contain instructions for location of input/outputfiles. See example contents in provided document readme.txtin Programming Assignment 1 link.
Points WILL BE taken if MINIMUM requirements andsubmissions not included.
1 point extra credit will be awarded if assignment iszipped for submission
Each solution is to be uniquely your own; minimal studentcollaboration allowed.
See Canvas and Syllabus for due dateTestData.txtTTFTFTTTFTFTFFTTFTTFTrent Watt T FTFTFTTTFTTFTTF TFMichael Strahan TTFTFTTTFTFTFFTTFTTFBad Robot TTFTFTTTFTFTFFTTF Jiminy Cricket TTTTFTTT TFTFFFTF Smelly Cat TTFTFTTTFTFTFFTTFTTFO No Stephen Strange FFTFTFFFTFTFTTFFTFFTThis is what i have so far!!
#include "student.h"
int main(){
Student student[30];ifstream inData;ofstream outData;string inFileName, outFileName;int data;
openFiles(inData, outData); if(inData.fail()) { // if not opened then display errormessage and close the program cout << "Error in opening "<< inFileName << endl; exit(1); } if(outData.fail()) { // if not opened then display errormessage and close the program cout << "Error in opening "<< outFileName << endl; exit(1); }
getData(inData, student , data); printData(student, data); closeFiles(inData, outData);
return 0;}
int openFiles(ifstream& input, ofstream& output){
string inFileName; string outFileName;
cout << "Enter the name of the file to beprocessed: "; cin >> inFileName; input.open(inFileName.c_str());
cout << "Enter the name of the output file:"; cin >> outFileName; output.open(outFileName.c_str());
return 0;}
void getData(ifstream& inData, Student list[], int&howMany){
while(inData) {
for (int i=0; i < howMany && !inData.eof(); i++)
inData.ignore(1000,'\n')>> list.firstName>> list.lastName>> list.testAnswers;
int count = 0;count++; }}
void printData(Student list[], int length){ for (int i = 0; i < length;i++) cout << setw(5) << list.firstName << setw(5) << list.lastName << setw(5) << list.testAnswers; cout << endl << endl;}
void closeFiles(ifstream& inData, ofstream& outData){ inData.close(); outData.close();}
char getLetterGrade(double pGrade){ char letter = 'F'; // assign F' to letter char(assume worst) if(pGrade >= 90.0000) // the numeric 'A' range letter = 'A'; // re-assignsoutput letter 'A; else if(pGrade >= 80.0000) // the numeric 'B' range letter ='B'; // re-assignoutput letter to 'B' else if(pGrade>= 70.0000) // the numeric 'C' range letter = 'C'; // re-assign output letter to 'C' else if(pGrade >= 60.0000) // thenumeric 'D' range (at or above) letter = 'D'; // re-assign output letter to 'D' return letter; // return thecorrect letter grade (in range)}
student.h file
#include <iostream>#include <fstream>#include <string>#include <iomanip>
using namespace std;
struct Student {
string firstName;string lastName;string testAnswers;int numericalScore;char letterGrade;
};
int openFiles(ifstream& inData, ofstream&outData);void getData(ifstream& inData, Student list[], int&length);void printData(Student list[], int length);void closeFiles(ifstream& inData, ofstream& outData);char getLetterGrade(double pGrade);
Functions, arrays of structures, arrays, file input, fileoutput, console output, output formatting, header file
Program description:
The input file contains the grade keyas the first row of data consisting of true and false answers,i.e., T and F:
TTFTFTTTFTFTFFTTFTTF
The remaining data in the filecontains a student’s first name, last name and answer to thetest. For example the second row of the filecontains:
Trent Watt T FTFTFTTTFTTFTTF TF
Read the first row into a variable,type of your choice, as the key to the correct answers. Output the key data to the console (cout).
Read the remaining file data andcompute the score by comparing the student answers to the gradekey. Each correct answer is 5 points. A space in thestudent answer set indicates the student did not answer thequestion. Read until end of file.
First Name
Last Name
Test Answers
Numerical Score
Letter Grade
Check the status of the file openfunctions. If the file does not open, display a message andexit the program.
The test has 20 questions. Eachcorrect answer is awarded five (5) points, each wrong answer orunanswered question gets zero (0) points.
Assume the following grade scale:
A: 90%-100%
B: 80%-89.99%
C: 70%-79.99%
D: 60%-69.99%
F: 0-59:99%
<A title of your choosing>
Name Score Letter Grade Student Answers
Ensure each line of data is neat andreadable. All score values must have two decimalplaces. Use manipulators such as setw, right and left to aidin neatess and readability..
Call the function from main. Declare the prototype in the header file. Code the functionheader and body after main.
Basic Comments:
Program Name: (name of programfile)
Author: (your name)
Brief explanation of programpurpose
Pre/Postconditions:
//Precondition: what must betrue about any parameters prior to function call. There maybe no preconditions
//Postconditions: what is trueafter function is complete
See pre/post condition comment examplein provided example program Documentation.cpp in ProgrammingAssignment 1 link.
Test and evaluate calculations for accuracy. Points will be taken for inaccurate calculations, improperformatting, directions not followed.
Turn in file code *.cpp; README file; header file *.h , inputfile and output file. You DO NOT need to submit theexecutable (*.exe). You MAY zip all the files and submit ifyou choose. You MUST include all the files indicated orpoints will be deducted.
README must contain instructions for location of input/outputfiles. See example contents in provided document readme.txtin Programming Assignment 1 link.
Points WILL BE taken if MINIMUM requirements andsubmissions not included.
1 point extra credit will be awarded if assignment iszipped for submission
Each solution is to be uniquely your own; minimal studentcollaboration allowed.
See Canvas and Syllabus for due dateTestData.txtTTFTFTTTFTFTFFTTFTTFTrent Watt T FTFTFTTTFTTFTTF TFMichael Strahan TTFTFTTTFTFTFFTTFTTFBad Robot TTFTFTTTFTFTFFTTF Jiminy Cricket TTTTFTTT TFTFFFTF Smelly Cat TTFTFTTTFTFTFFTTFTTFO No Stephen Strange FFTFTFFFTFTFTTFFTFFTThis is what i have so far!!
#include "student.h"
int main(){
Student student[30];ifstream inData;ofstream outData;string inFileName, outFileName;int data;
openFiles(inData, outData); if(inData.fail()) { // if not opened then display errormessage and close the program cout << "Error in opening "<< inFileName << endl; exit(1); } if(outData.fail()) { // if not opened then display errormessage and close the program cout << "Error in opening "<< outFileName << endl; exit(1); }
getData(inData, student , data); printData(student, data); closeFiles(inData, outData);
return 0;}
int openFiles(ifstream& input, ofstream& output){
string inFileName; string outFileName;
cout << "Enter the name of the file to beprocessed: "; cin >> inFileName; input.open(inFileName.c_str());
cout << "Enter the name of the output file:"; cin >> outFileName; output.open(outFileName.c_str());
return 0;}
void getData(ifstream& inData, Student list[], int&howMany){
while(inData) {
for (int i=0; i < howMany && !inData.eof(); i++)
inData.ignore(1000,'\n')>> list.firstName>> list.lastName>> list.testAnswers;
int count = 0;count++; }}
void printData(Student list[], int length){ for (int i = 0; i < length;i++) cout << setw(5) << list.firstName << setw(5) << list.lastName << setw(5) << list.testAnswers; cout << endl << endl;}
void closeFiles(ifstream& inData, ofstream& outData){ inData.close(); outData.close();}
char getLetterGrade(double pGrade){ char letter = 'F'; // assign F' to letter char(assume worst) if(pGrade >= 90.0000) // the numeric 'A' range letter = 'A'; // re-assignsoutput letter 'A; else if(pGrade >= 80.0000) // the numeric 'B' range letter ='B'; // re-assignoutput letter to 'B' else if(pGrade>= 70.0000) // the numeric 'C' range letter = 'C'; // re-assign output letter to 'C' else if(pGrade >= 60.0000) // thenumeric 'D' range (at or above) letter = 'D'; // re-assign output letter to 'D' return letter; // return thecorrect letter grade (in range)}
student.h file
#include <iostream>#include <fstream>#include <string>#include <iomanip>
using namespace std;
struct Student {
string firstName;string lastName;string testAnswers;int numericalScore;char letterGrade;
};
int openFiles(ifstream& inData, ofstream&outData);void getData(ifstream& inData, Student list[], int&length);void printData(Student list[], int length);void closeFiles(ifstream& inData, ofstream& outData);char getLetterGrade(double pGrade);