Error passing an array of structs
The goal of this program is to create an array of structs that holds a dynamic array of students and allows you to put in their name, student ID, major, and GPA.
The code i have right now just does a flat number of students, but I'm having issues with passing the struct into the function.
Student.h
Code:
// Global constants
const int STUDENT_NAME = 40; // Max number of characters for a students name
const int MAJOR_NAME = 40; // Max number of characters for a students major
const int STUDENT_YEAR = 20; // Max number of characters for a students year
// Declaration of our Student Structure
struct Student
{
char name[STUDENT_NAME]; // Stores full name of student
char major[MAJOR_NAME]; // Stores major of student
char year[STUDENT_YEAR]; // Stores year of student (Freshman, Sophomore, Junior, Senior)
int studentID; // Unique number to identify each student
float GPA; // Students Grade Point Average
};
// Declaration of our class
class myRoster
{
public:
void FillRoster(Student CourseRoster[], int classSize);
};
Main.cpp
Code:
#include <iostream>
#include "Student.h"
using namespace std;
// Constants used througout the program
const int CLASS_SIZE = 6; // Class/Roster size
const int CLASS_TITLE = 41; // Max size of a class name. Extra space for the NULL character.
const int CLASS_NUMBER = 9; // Max size of the class number name. Extra space for the NULL character.
int main()
{
// Create my strut object
Student CourseRoster[CLASS_SIZE];
// Create my class object
myRoster roster;
// Create variables to hold the class name and the course number.
char className[CLASS_TITLE];
char classNumber[CLASS_NUMBER];
// Get the course name and course number from the user. The cin.ignore lines
// ensure that we only take up to the maximum number of characters allowed by that
// field so that we don't accidentally try to create a course name with 60
// characters or a course number with 15 characters.
cout << "Enter the name of the course. > ";
cin.getline(className, CLASS_TITLE);
cin.ignore(cin.rdbuf()->in_avail(), '40');
cout << endl << "Enter the course number. > ";
cin.getline(classNumber, CLASS_NUMBER);
cin.ignore(cin.rdbuf()->in_avail(), '8');
// Call the FillRoster Function
roster.FillRoster(CourseRoster, CLASS_SIZE);
system("pause");
return 0;
}
Implementation.cpp
Code:
#include <iostream>
#include "Student.h"
using namespace std;
// FillRoster will take each student in the class (in our case, 6) and
// populate their name, their major, their student ID, and their GPA.
void FillRoster (Student CourseRoster[], int classSize)
{
//
for (int i = 0; i < classSize; i++)
{
cout << endl << "Enter the students name. > ";
cin >> CourseRoster[i].name;
cout << endl << "Enter the students major. > ";
cin >> CourseRoster[i].major;
cout << endl << "Enter the students ID number. > ";
cin >> CourseRoster[i].studentID;
/* REMINDER - ENTER ADDITIONAL CODE TO DETERMINE IF THE STUDENT
ID IS A VALID STUDENT ID. STUDENT ID MUST BE 5 CHARACTERS LONG,
AND NOT A NEGATIVE NUMBER. STUDENT ID NUMBERS MUST BE BETWEEN
10000 AND 99999. */
cout << endl << "Enter the students GPA. > ";
cin >> CourseRoster[i].GPA;
/* REMINDER - ENTER ADDITIONAL CODE TO DETERMINE IF THE GPA
ID IS A VALID OR NOT. GPA MUST BE BETWEEN 0.0 and 4.0 IN
VALUE. */
}
return;
}
The program appears to error out on the roster.FillRoster(CourseRoster, CLASS_SIZE); line.
I'm sure it's something fairly simple, but frankly, after looking at it and trying stuff for a couple hours, I need a new pair of eyes on it because I'm overlooking the issue.
Thanks in advance to all!