Online Lab problem

I need help with a lab assignment that will be due by the end of the week. It's suppose to be simple, but I am not getting it, and no, don't give me guff about 'doing my homework for me', because homework should actually give you a better idea of how to do something, rather than confuse you.

Here's the instructions:

Your program for this lab exercise should be fairly simple. It will be a
program that accepts a list of student records and then prints out some
statistics. Each student record consists of a last name, major, and GPA.
Student records should be represented using a Student class. The main program
should have two phases.

Phase 1: Data entry -- ask the user how many students there will be and then
prompt the user to enter the three data items for each student. All of the
student data should be stored in an array. There is no max limit to the
number of students. The program should not hardcord the different available
majors.

Phase 2: Statistics -- After all student records are entered the program
should print a report as follows:

Total number of students: x
Average GPA for all students: y
Number of different majors: z
Average GPA per major:
major 1: a
major 2: b
major 3: c

In the report replace each of the values x, y, z, a, b, c with actual values.
Also note that the "Average GPA per major" will have a separate row for each
major (no matter how many there are).

-- Getting started

Before getting too involved in writing the code you should start by creating
the three files you need (main.cpp, student.h, student.cpp) and make sure
you can compile all of them.

If I can get some help please, I would appreciate it. I am just stumped on this.
Last edited on
Okay, I have some code to help you along:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "student.h"

using namespace std;

int main() {

	int choice;
	bool done = false;
	int x, y, z;

	doStudentDataEntry();
	doGPAScores(); // Just a filler for when I have to do Average GPA for all students, and for the majors

	cout << "Total number of students: " << x;
	cout << "Average GPA for all students: " << y;
	cout << "Number of different majors: " << z;
	cout << "Average GPA per major: \n";
	// Need help making a line for each major giving their average GPAs
}


student.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>

class Student
{
	private:
		string lastName;
		double GPA;
		string major;
	public:
		Student();
		Student(string name, double GPA, string major);
		string getName();
		double getGPA();
		string getmajor();
		void print();
};

void doStudentDataEntry();
void doGPAScores();

#endif 


student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "student.h"
#include <iostream>
#include <cmath>

using namespace std;

void doStudentDataEntry()
{
	int i, x;
	cout << "How many students are in your class?";
	cin >> x;
	int *StudentGPA = new int[x];
	for (i = 0; i < x; i++)
	{
		cout << "Insert the last name of the student: ";
		// stuck on what to do regarding the class from the header, and putting it into a array.
	}
}

void doGPAScores()
Is there anyone that can help me, please?
// stuck on what to do regarding the class from the header, and putting it into a array.


http://www.cplusplus.com/reference/vector/vector/

You can use a vector to store a list of students, it's easier to use than a regular array.

in your case you can just use something like this:

1
2
3
vector<Student> studentList;
Student temporaryStudent("bob",3.5,"chemical barbecuing");
studentList.push_back(temporaryStudent);
Last edited on
@wizebin,
All of the student data should be stored in an array.


@Dragonov,

I would get the number of students in main and declare an array of students. Then pass this array to the function doStudentDataEntry.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main() 
{
   int num_students;

   cout << "Number of students: ";
   cin >> num_students; 

   Student *students = new Student[num_students];

   doStudentEntry(students, num_students);
}

void doStudentEntry(Student *students, int num_students)
{
   for (int i = 0; i < num_students; i++)
  {
      // get data from the user and store it in students[i].
     // for example string name; cin >> name; students[i].setName(name);
   }
}


Hope this help you to get started
Do I put the int main in the student.cpp, or the main.cpp? Because in the main.cpp, I have to be able to return the num_students into the main.cpp's number of students value.

Btw, like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void doStudentEntry(Student *students, int num_students)
{
	for (int i = 0; i < num_students; i++)
	{
		
		cout << "Insert the name of the student: ";
		cin >> Student[i].getName(lastName);
		cout << "Insert the student's major: ";
		cin >> Student[i].getmajor(major);
		cout << "Insert the student's GPA: ";
		cin >> Student[i].getGPA(GPA);
		// get data from the user and store it in students[i].
		// for example string name; cin >> name; students[i].setName(name);
	}
}
int main( /* void or otherwise */ ) should, as the name implies, go into Main.cpp.

Also from how you're phrasing your question, it sounds like you should create a counter that increments once every time a student is added. Or you could make a function that does that. Personally, I would probably integrate the counter into whatever function you're using to add a student as part of your Student class.
Well, I am trying to make it to where I insert a value of how many students I want, and then have it have me enter a lastname, a major, and it's GPA for each student.
Hi,

Not sure if you still need any, but
If you do need help on understanding the code, I could give it a shot. I've pretty much had over two years experience of tutoring post-secondary students in C++ and C#.

Let me know at sparkprogrammer@gmail.com

Joe

Okay, everyone. You will be happy to know that I had managed to make major headway on the assignment, but I have one more bit that i need help with: making this part of the code work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
double AvgGPAperMajor(Student x[], int numStudents)
{
	double total = 0;
	double average = 0;
	int nummajors;
	string major;
	int majorfound = 0;
	bool foundItem = false;
	for (int i = 0; i < numStudents; i++)
	{
		string *ListMajors = new string[numStudents];
		x[i].getmajor;
		ListMajors[i] = x[i].getmajor;
		for (int j = 0; j < majorfound; j++)
		{
			if (x[i].getmajor == ListMajors[j])
			{
				foundItem = true;
			}
		}
		if (foundItem = false)
		{
			ListMajors[majorfound] = x[i].getmajor;
			majorfound++;
		}
		for (int k = 0; k < majorfound, k++)
		{
			ListMajors[k] = x[i].getmajor;
			if (ListMajors[i] == ListMajors[k])
			{
				total = total + x[i].getGPA;
				nummajors++;
			}

			average = total / nummajors;
			cout << ListMajors[i] << ": " << average;
		}
		return;
	}


Basically, I need it to calculate the average GPA for each major, and print it out for each one. One hint I was given was to find out where to store the List of majors. If anyone can impart knowledge on what to do next, I am all ears.
Topic archived. No new replies allowed.