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");
}

Several points.
1 - strings - you're setting up C-style, char array based string, but not using the string functions for any manipulations. Look up this topic, especially strcpy( ).

2. Your constructor is allocating (again) a string and double - all it should do is set intitial values to the string and double that are the class private members. (see 1 above)

3. Your parameterized constructor, again see 1 above.

4. The big loop in main() should be refering to sptr+i, not +1

That should be enough to keep you busy a while.

I still am lost as what to do, I don't think I can use the strcpy() because we haven't gone over string functions in class. We just started to do pointers and the homework up to this point has been pretty tough(I have no background programming experience).

Or, if you prefer and are allowed, since you include the string header file you can change the declaration of name to type string like this:

string name;

rather thn using C style strings as you have done. Some of the advantages of the string class as opposed to using C style strings is that direct assignment and direct comparison is possible so you don't need to use the functions in the cstring header file that allows you to manipulate and evaluate C style strings.

If you can't use the string class, then you are going to need to use the functions in cstring to manipulate C style strings.

I have figured out all the code except for the FindAverage. I do not know what I would need to do to get the function to bring the datd from the class into the function do the arithmetic and then return the average into my main.

Based on the function prototype you have commented out it looks like you need to declare an array of Student, sptr looks like it should work. Then loop through sptr accessing the grade variable of each student and add to a running total. When the loop is done then divide the running total by the number of students to get the average grade of the students listed in the array. You can return that value to main and store it in a variable of type double if you wish, or you use the return value directly in an output statement if you wish.

I'm still having trouble getting my findaverage function. This is the code I have so far:

#include <iostream>
#include <iomanip>

using namespace std;

double FindAverage(class Student[], int num);

class Student
{
private:

	char name[20];
	double grade;

public:

	Student() //Default Constructor
	{
		for(int i=0;i<20;i++)
		{
			name[i] = ' ';
		}

		double grade =0;
	}

	Student(char new_name[20], double new_grade)//The Constructor
	{
		for(int i=0; i<20; i++)
		{
			if(new_name[1] !='/0')
				name[i]=new_name[i];
			else
				name[i]=' ';
		}

		grade = new_grade;

		cout << " The Constructor" << endl;
	}

	~Student(){} //The Destructor

	///////////////////////////////////////////////////
	//
	//Function that returns a grade
	//
	///////////////////////////////////////////////////	

	double GetGrade()
	{
		return grade;
	}

	///////////////////////////////////////////////////
	//
	//Function that sets a grade
	//
	///////////////////////////////////////////////////

	void SetGrade(double new_grade)
	{
		grade = new_grade;
	}

	///////////////////////////////////////////////////
	//
	//Function that returns a name
	//
	///////////////////////////////////////////////////

	char GetName()//Function that returns a name
	{
		for(int i=0; i<20; i++)
		{
			return name[i];
		}	
	}

	///////////////////////////////////////////////////
	//
	//Function that sets a name
	//
	///////////////////////////////////////////////////
	
	void SetName(char new_name[20])
	{
		for(int i=0; i < 20; i++)
		{
			name[i] = new_name[i];
		}
	}

	/////////////////////////////////////////////////
	//
	//Function that will send the user collected
	//data to the screen.
	//
	////////////////////////////////////////////////

	void Print()
	{
		cout<< name << " " << setw(4)<< grade;
		cout<< endl;
	}

};
////////////////////////////////////////////////
//
//Main Function
//
///////////////////////////////////////////////
int main()
{
	int num ; //num is equal to how many students are in the classroom
	char name[20]; //
	double grade;



	cout << "Enter the size of your class: ";
	cin >> num;

	Student *sptr = new Student[num];

	for(int n=0; n < num; n++) //Collects the users data and returns it to the class
	{
		cout << "Enter Students name: ";
		cin >> name;
		cout << "Enter Students grade: ";
		cin >> grade;
		(sptr+n)->SetName(name);
		(sptr+n)->SetGrade(grade);	
	}

	for(int i=0; i<num; i++) //Calls the class to print the user collected data
	{
		(sptr+i)->Print();
	}

	cout << "The class of " << num << " the average is "<< endl; //Statement that prints the amount of students in the classroom and the average.

	system ("pause");
}
//////////////////////////////////////////////
//
//Find Average Function will find the average
//Grades of the students and return it to the
//Main function
//
//////////////////////////////////////////////

double FindAverage(Student[],int num)
{	
	
	double average;
	double total;

	for(int i=0; i< num; i++)
	{
		total= (Student::GetGrade)+ num;
	}

	average = total/num;

	return average;
}
double FindAverage(Student /*need a name here*/[],int num)
{	
	
	double average;
	double total;  //initialize this to zero before starting to add to it

	for(int i=0; i< num; i++)
	{
		total= (Student::GetGrade)+ num; //why add num?
                                               //GetGrade is a function, needs ()
                                               //don't use class name Student, use 
                                               //the parameter name you should add above
	}
	average = total/num;

	return average;
}

Your GetName function is not gonna work - you really need to be using string functions. I don't see how you could be at a point in class of writing classes and not having covered that topic.

Well, I figured it out without using any string functions, and I don't know why we haven't yet to learn any of them yet. We just started OOP and not much else aside from basics.

This article has been dead for over six months. Start a new discussion instead.