I am new to the fascinating world of C++, I should of stuck with Spanish.. But I have an assignment due and I am so close I can taste it. I am having an issue with my global functions.
I can not get my average function nor my maxGrade function to work.
Any help would be greatly appreciated.
Here is what I have thus far:
//Libraries
#include <iostream>
#include <cstdlib>
//Line to tell rest of program to use namespace standard
using namespace std;
//functions prototypes
float averageGrade(int studentInfo[], int size);
int maxgradestudent(int studentInfo[], int size);
//classes declaration
class Person
{
public:
Person(void);
~Person(void);
void Setfirstname(char []);
char * Getfirstname(void);
void Setlastname(char []);
char * Getlastname(void);
private:
char Firstname[30];
char Lastname [30];
};
class Student : public Person
{
public:
Student(void);
~Student(void);
void Setnid(char[]);
char * getNID(void);
void SetGrade(float);
float GetGrade(void);
private:
float grade;
char NID [30];
};
void main()
{
Student studentInfo[100];
float studentGrade;
int i,numStudents;
char firstName[30];
char lastName[30];
char NID [30];
float ave =0.0;
int maxgrade;
//check for errors
do
{
cout << "Enter the number of students:";
cin >> numStudents;
if ((numStudents < 1) || (numStudents > 100))
{
cout << "Incorrect value. Try again...\n";
}
} while ((numStudents < 1) || (numStudents > 100));
//Enter info for students:
for (i=0;i<numStudents;i++)
{
cout << "Student #"<<i+1 << endl;
cout << " Enter the Student's first name :";
cin >> firstName;
cout << " Enter the Student's last name :";
cin >> lastName;
cout << "Enter the student's nid:";
cin >> NID;
cout << "Enter the student's grade:";
cin >> studentGrade;
//send data to functions
studentInfo[i].Setfirstname(firstName);
studentInfo[i].Setlastname(lastName);
studentInfo[i].Setnid(NID);
studentInfo[i].SetGrade(studentGrade);
//average call fuction
ave= averageGrade(studentInfo, ave);
//maximum call function
maxgrade= maxgradestudent(studentInfo, ave); }
cout << "\n\n";
for (i=0;i<numStudents;i++)
{
cout << "The average grade is " << studentInfo[i].GetGrade() << endl;
cout << "The student with the highest grade is :" << studentInfo[i].Getfirstname()<<" ";
cout << studentInfo[i].Getlastname()<< "NID:"<< studentInfo[i].getNID()<<"grade:"<<studentInfo[i].GetGrade()<<"\n";
}
}
//FUNCTIONS
//constructor
Person::Person(void)
{
strcpy(Firstname,"");
}
//destructor
Person::~Person(void)
{
strcpy(Firstname,"");
}
//function for first name
void Person::Setfirstname(char firstname[])
{
strcpy(Firstname,firstname);
}
//function for Last name
void Person::Setlastname(char lastname[])
{
strcpy(Lastname,lastname);
}
char * Person::Getfirstname(void)
{
return Firstname;
}
char * Person::Getlastname(void)
{
return Lastname;
}
//nid function
Student::Student(void)
{
strcpy (NID,"");
}
Student::~Student(void)
{
strcpy (NID,"");
}
void Student::Setnid(char Nid[])
{
strcpy (NID,Nid);
}
char * Student::getNID(void)
{
return NID;
}
//function for grade:
void Student::SetGrade(float studentGrade)
{
grade = studentGrade;
}
float Student::GetGrade(void)
{
return grade;
}
float averageGrade(int studentScores[], int size)
{
int total = 0; //variable to keep sum of all elements in array
for(int i=0; i<size; i++)
{
total=total+studentScores[i]; //add this students score to the total
}
return (float)total/size;
}