I hate to come back to this again, but I've got a bug that I can't find again. This time the program compiles and runs, but when going thru the FillRoster loop more than once, the program always skips asking for the 2nd, 3rd, 4th, etc... student name.
Here is the (mostly) finished code:
Student.h
Code:
const int STUDENT_ID = 5;
// Declaration of our Student Structure
struct Student
{
char name[STUDENT_NAME]; // Stores full name of student
char major[STUDENT_MAJOR]; // 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);
float AverageGPA (Student CourseRoster[], int classSize);
void OutputRoster (Student CourseRoster[], int classSize, char *pClassName, char *pClassNumber);
};
Implementation.cpp
Code:
#include <iostream>
#include <iomanip>
#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 myRoster::FillRoster (Student CourseRoster[], int classSize)
{
for (int i = 0; i < classSize; i++)
{
// Get Students Name
cout << endl << "Enter the students name. > ";
cin.getline(CourseRoster[i].name, STUDENT_NAME);
cin.ignore(cin.rdbuf()->in_avail(), '40');
// Get Students Major
cout << endl << "Enter the students major. > ";
cin.getline(CourseRoster[i].major, STUDENT_MAJOR);
cin.ignore(cin.rdbuf()->in_avail(), '40');
do
{
// Get Students Student ID.
CourseRoster[i].studentID = 10000;
cout << endl << "Enter the students ID number (Between 10000 and 99999). > ";
cin >> CourseRoster[i].studentID;
}
while (CourseRoster[i].studentID < 10000 || CourseRoster[i].studentID > 99999);
do
{
// Get Student GPA
cout << endl << "Enter the students GPA. > ";
cin >> CourseRoster[i].GPA;
}
while (CourseRoster[i].GPA < 0.0 || CourseRoster[i].GPA > 4.0);
}
return;
}
float myRoster::AverageGPA (Student CourseRoster[], int classSize)
{
// Declare variable to hold the average GPA
float avgGPA = 0.0;
for (int j = 0; j < classSize; j++)
{
avgGPA = avgGPA + CourseRoster[j].GPA;
}
avgGPA = avgGPA / classSize;
return avgGPA;
}
void myRoster::OutputRoster (Student CourseRoster[], int classSize, char *pClassName, char *pClassNumber)
{
// Print out all the relevant information of the class and its students
cout << endl << endl << "Roster for " << pClassName << ", Course ID " << pClassNumber << " is as follows: ";
cout << endl << endl;
cout << "NAME \t\t STUDENT ID \t\t ACADEMIC MAJOR \t GPA" << endl << endl;
for (int k = 0; k < classSize; k++)
{
cout << CourseRoster[k].name << " \t\t " << CourseRoster[k].studentID << " \t\t\t "
<< CourseRoster[k].major << " \t\t\t " << CourseRoster[k].GPA << endl;
}
return;
}
Main.cpp
Code:
#include <iostream>
#include <iomanip>
#include "Student.h"
using namespace std;
// Constants used througout the program
const int CLASS_SIZE = 2; // 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 needed by the program. className holds the name of the class,
// classNumber holds the class abbreviation (CS115), avgGPA holds the average GPA
// of the students in the class. pAvgGPA is a pointer to avgGPA.
char className[CLASS_TITLE];
char classNumber[CLASS_NUMBER];
char *pClassName = &className[0];
char *pClassNumber = &classNumber[0];
float *pAvgGPA = NULL;
// 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);
// Calculate the Average GPA of the students
cout << "The average GPA for the students in " << className << "is "
<< roster.AverageGPA(CourseRoster, CLASS_SIZE);
// Output the class information
roster.OutputRoster(CourseRoster, CLASS_SIZE, pClassName, pClassNumber);
system("pause");
return 0;
}
Output: