I have written a c++ program that takes input from a file and outputs the average. The program uses structs and I need to convert the struct to a class.
I just dont know how to get started and if I will have to re-write my whole program.
would it be easier to put the class program in separate files
I just dont know how to get started and if I will have to re-write my whole program.
Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
ifstream in_file;
//structure that holds student record information.
typedef struct records{
string name;
float quizes[7];
float projects[6];
float exams[2];
float labs[14];
};
const int num_quizes = 7;
const int num_exams = 2;
const int num_projects = 6;
const int num_labs = 14;
const int quizes_pts = 70;
const int exams_pts = 200;
const int projects_pts = 300;
const int labs_pts = 150;
void get_scores(ifstream& infile, int num_scores, float scores[]);
double get_average(float scores[], int num_scores, int tot_pts, float& average);
void total_average(float average,float& student_average);
void grades_display(int counter,float ave1,float ave2,float ave3,float ave4,float total,char grade);
int main()
{
cout << "No. Name Quiz Project Exam Lab Total Grade"
<< endl;
cout << " --- --------- ----- ---------- ------ ----- ------- -----"
<< endl;
cout << endl;
records grades;
ifstream in_file;
// Define a file input stream and open the file
in_file.open("record.txt"); //opening file
if (in_file.fail())
{
// Failed to open the file (file doesn't exist or isn't readable)
cout << "Could not open file: "<< "\n";
exit(1);
}
float average = 0;
int counter = 1;
while(!in_file.eof())
{
//get string from the file
in_file >> grades.name;
char letter_grade;
float student_average = 0;
// Repeatedly get characters from the file
get_scores(in_file, num_quizes, grades.quizes); //function for inputing info in struct.
get_average(grades.quizes, num_quizes, quizes_pts, average); //function for average
total_average(average,student_average);
float quiz_average = average;
get_scores(in_file, num_projects, grades.projects); //input project scores
get_average(grades.projects, num_projects, projects_pts, average);
total_average(average,student_average);
float project_average = average;
get_scores(in_file, num_exams, grades.exams); //input exam scores
get_average(grades.exams, num_exams, exams_pts, average);
total_average(average,student_average);
float exams_average = average;
get_scores(in_file, num_labs, grades.labs); //input lab scores
get_average(grades.labs, num_labs, labs_pts, average);
total_average(average,student_average);
float labs_average = average;
student_average = student_average/4;
grades_display(counter,quiz_average,project_average,exams_average,labs_average,student_average,letter_grade);
counter++;
}
// Close the file
in_file.close();
return 0;
}
//function that takes in parameters to input scores in array
void get_scores(ifstream& in_file, int num_scores, float scores[])
{
for (int i = 0; i < num_scores; i++)
{
in_file >> scores[i];
}
}
//funtion that takes the average of the inputed scores
double get_average(float scores[], int num_scores,int tot_pts,float& average)
{
float a = 0;
for (int i = 0; i < num_scores; i++)
{
a = scores[i] + a;
}
average = (a/tot_pts);
average = average * 100;
return average;
}
//function to take total average for each member
void total_average(float average,float& student_average)
{
student_average = average + student_average;
}
//function to output letter grade and output scores
void grades_display(int counter,float ave1,float ave2,float ave3,float ave4,float total,char grade)
{
if(90 < total)
grade = 'A';
if((80 < total)&&(total < 90))
grade = 'B';
if((70 < total)&&(total < 80))
grade = 'C';
if((60 < total)&&(total < 70))
grade = 'D';
if (total < 60)
grade = 'F';
cout << " " << counter << " " << ave1 << " " << ave2 << " " << ave3 << " " << ave4 << " " << total << " " << grade;
}
would it be easier to put the class program in separate files
Comment