Hello, I'm currently taking a programming class in college for my first time and I am having a hard time with my assignment for the week. I have to write a program that I create a class called Student. It will then have two members within the private sector called char name[20] and double grade. aside from the get,set,constructor,destructor, and then a function that will print it.
In the main fucntion I need to ask the user for the number of students and creat a dynamic array. For each of the student objects I need the name and grade.
I was given this by my professor to use: Student* sptr = new Student[num]; and I also need to have a function outside the class and main to find the average of the grades. and return that to the screen.
So far I have written my class and some of the main function. I am just stuck as to where to go from the existing code that I have.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//double FindAverag(Student[]);
class Student
{
private:
char name[20];
double grade;
public:
Student() //Enables the object of the class to be initialized
{
char name[20];
double grade =0;
}
Student(char student[20], double num)//The Constructor
{
name[0] = student[0];
grade = num;
cout << " The Constructor" << endl;
}
~Student(){} //The Destructor
double GetGrade()//Function that returna a grade
{
return grade;
}
void SetGrade(double num)//Function that sets a grade
{
grade = num;
}
int GetName()//Function that returns a name
{
return name[20];
}
void SetName(char student[20])//Function that sets a name
{
/*for(int i=0;i<20;i++)
{
if(student[i]!='\0')
name[i] = student[i];
else
name[i] = ' ';
}*/
name[20]=student[20];
}
/////////////////////////////////////////////////
//
//Function that will send the information to the
//Screen, and get the information sent from the
//FindAverage function
//
////////////////////////////////////////////////
void Print()
{
cout<< &Student::GetName <<setw(4)<< &Student::GetGrade<<"\n";
}
};
////////////////////////////////////////////////
//
//Main Function
//
///////////////////////////////////////////////
int main()
{
int num ;//num is equal to how many students are in the classroom
char name[25];
double grade;
cout << "Enter the size of your class: ";
cin >> num;
Student *sptr;
sptr = new Student[num];
for(int n=0; n < num; n++)
{
cout << "Enter Students name: ";
cin >> name;
cout << "Enter Students grade: ";
cin >> grade;
(sptr+1)->SetName(name);
(sptr+1)->SetGrade(grade);
}
for(int i=0; i<num; i++)
{
(sptr+i)->Print();
}
system ("pause");
}