C++ structure program that returns student information from file

I have an unfinished program for homework, the main issue is I am not sure how to make a function that returns the average of an array of test scores and homework scores, so I can use those averages to use in my compute_grade function. I would also like some input on ways I can improve my program. I have also commented out what I am trying to do in the program to help others understand it.
Data File:

LAST NAME	FIRST NAME	BIRTH DATE	ID	CLASS	MAJOR	HOMEWORK SCORES 	EXAM SCORES
Jones		Shelia		06 21 1992	3472	FR	CS	87 89 65 90 91 93 79	91 94 90
Bain		Robert		11 06 1991	1754	SO	CS	77 88 99 95 93 86 78	83 95 87
Smith		Pamela		07 18 1990	1888	SO	CS	87 92 95 92 94 88 89	79 85 89
Hall		Sherry		08 07 1993	7640	SR	BIO	85 89 75 83 91 95 92	91 81 84
Stokes		Jerry		06 25 1990	2311	JR	CS	83 89 85 88 91 93 95	85 95 90
Clayton		Randy		05 21 1992	9077	JR	EE	80 86 95 89 97 83 96	78 83 90
Chancellor	Janet		02 28 1991	2341	SO	CHEM	87 99 95 88 97 90 85	82 88 94
Adams		Mark		04 16 1989	1764	FR	CS	96 97 99 91 93 89 98	95 97 96
Hooten		Lamar		10 31 1988	8734	SO	CS	91 89 77 90 91 90 84	91 85 86
Mintor		Lara		10 31 1993	8734	FR	MATH	88 85 79 93 92 81 86	95 82 89

Prompt:
Problem 2: Structures (struct) (35 Points)

Last Name First Name Birthdate Number Class Major Homework Scores (7) Exam Scores (3)

Define a structure for a student record. (Note that the Homework and Exams are arrays of numbers. There are 7 homework scores and 3 exam scores.)
Declare an array of student records to hold all the data from the file.
Read in the student data from the file to initialize the array of student records.
Write a function that will compute the grade for a given student. (Grade should be one of the members of the struct you define.)
Grade = (average of homework) * 0.40 + (average of exams) * 0.60
(Note: Your program will have to calculate the homework average and the exam average.)
Call the function to calculate the grades for all students.
Write a print function that will print out the following information for a student:
Last Name
First Name
Birthdate
Number
Major
Homework Average (Not each individual homework score)
Exam Average (Not each individual exam score)
Grade
In the remainder of your program, inside a loop, do the following:
Prompt the user to enter the firstname and lastname for a student.
Print out to the screen (you can call the print function) the data corresponding to the student.
TEST CASES: Pamela Smith and Mark Adams

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;

struct date
{
	unsigned int month;
	unsigned int day;
	unsigned int year;
};
// Define a structure for a student record. (Note that the Homework and Exams are arrays of numbers. 
// There are 7 homework scores and 3 exam scores.)
struct student_record
{
	string firstname;
	string lastname;		
    date birthdate;
    string year;
    string major;
	string ID;
	double exam_grades[6];
	double homework_grades[2];
	float grade;
	};
// Declare an array of student records to hold all the data from the file.
// Read in the student data from the file to initialize the array of student records.
    const int RECORDS = 10;

void compute_grade(student_record &student);
void print_record(student_record student);

int main(void)
{
    //Call the function to calculate the grades for all students.
    ifstream infile;
    
    student_record student;
    string dataline;
    int i = 0, k = 0, count = 0;
	cout << "Student records" << endl << endl;
	cout << "Open the data file." << endl << endl;
	infile.open("/Users/adam/desktop/temp/student_records.txt");

    if (!infile) 
	{
        cout << "Unable to open the input file. " << endl;
        return(1);
	}
	else
	{
	    cout << "Data file opened." << endl;
	}

	cout << "Read line one: Column Headings " << endl;
	getline(infile,dataline);

	// Close the files
	infile.close();



    cout << endl << endl;


}


// Write a function that will compute the grade for a given student. 
// (Grade should be one of the members of the struct you define.)
// Grade = (average of homework) * 0.40 + (average of exams) * 0.60
// Note: Your program will have to calculate the homework average and the exam average.)
void compute_grade(student_record student)
{
	cout << "In function compute grade." << endl;
	cout << "Exams Average= " << student.exam_grades << endl;
	cout << "Homeworks Average= " << student.homework_grades << endl;
	student.grade = student.homework_grades * 0.40 + student.exam_grades * 0.60;
	cout << "Grade = " << student.grade << endl;

	return;
}
//	Write a print function that will print out the following information for a student:
void print_record(student_record student)
{
    cout << student.lastname << " " << student.firstname << endl;
	cout << "Date of birth " 
		 << setw(3) << student.birthdate.month 
		 << setw(3) << student.birthdate.day 
		 << setw(5)<< student.birthdate.year
		 << endl;
	cout << "ID: " << student.ID << endl;
	cout << "Exam Average: " << student.exam_grades << endl;
	cout << "Homework Average: " << student.homework_grades << endl;
	cout << "Grade: " << student.grade << endl;
    return;
}
//In the remainder of your program, inside a loop, do the following:
//Prompt the user to enter the firstname and lastname for a student.
//Print out to the screen (you can call the print function) the data corresponding to the student. 
while it is possible to return many values from a function (you can return a struct from a function, for one example of how you can do that); modular code is better.

consider
1
2
3
4
5
6
7
8
9
double average (double * arrr; int size)
{
    double result{};
    for(int i = 0; i < size; i++)
      result += arr[i];
    return result/size;
}
homeworkavg = average(homework, number_assignments);  //pass in the variable eg student.homework_grades here. 
testavg = average(tests, number_tests);


line 81 looks suspect ... arrays need to be accessed via [] (my code above is an exception, the array name can be used as a pointer which is what I am doing). You usually do this in a loop. I went ahead and did average for you above to illustrate this. You need to do like that where you try to use the arrays. You can't say array * 5 to multiple all items in the array by 5; C (raw arrays are C) isn't that nice. I think <valarray> can do something like this, but its probably not something you want to get into right now.
Last edited on
There are 7 homework scores and 3 exam scores - but you are ony defining arrays for 6 and 2!

I would suggest that next you write a functions to read one record from the file and display it. Then have a loop that correctly reads and displays the whole file. Once you have that working, then you can start on averages, grades etc.
@seeplus I thought the count started from 0 which is why I used 2 and 6 for the arrays
I thought the count started from 0 which is why I used 2 and 6 for the arrays

Arrays are accessed starting at zero, but you still need to define arrays with the number of elements they will hold.

If an array is going to hold 7 elements you define it as (example): int arr[7];
The elements are accessed from arr[0] (the first element) to arr[6] (the seventh).
0,1,2,3
how many numbers do you see up there, 3 or 4?
the array size is 4. it is accessed by 0,1,2,3
Topic archived. No new replies allowed.