I've long looked at this site for advice, but I'm stuck now. I have an idea of WHAT I need to do, but don't know exactly how to do it.

YES this is homework. NO I'm not asking for anyone to completely do it because I need to learn this stuff. YES I am asking for help and I'm not the brightest bulb so I need specifics...

Here's my problem:
1. Define the class Address that has 4 data members:
• streetAddress: a string of a maximum of 50 characters
• city: a string of a maximum of 30 characters
• state: a string of a maximum of 30 characters
• ZIP: integer.
In addition to the data members, the class has functions to “set” and “get” the
values of the data members as well as a function to display the contents of all the
data members.
a) Write the header file Address.h for this class. Your class definitions should
not allow any function outside the class to access the data members.
b) Write the implementation file Address.cpp which implements the member
functions of the Address class.
2. Define a class Student that has the following data members:
• studentName: a string of a maximum of 30 characters
• studentID: an integer that is unique for each student and is dynamically
created when a new Student object is created.
• studentAddress: an object of the Address class
• GPA: a floating number
In addition to the data members, the class has functions to “set” and “get” the
values of the data members as well as a function to display the contents of all the
data members.
a) Write the header file Student.h for this class. Your class definitions should not
allow any function outside the class to access the data members.
b) Write the implementation file Student.cpp which implements the member
functions of the Student class.
3. Develop a “Course Roster Application” that allows the user to:
• create a course roster of any number of students. (use dynamically
allocated array of objects)
• enter information for each student.
• display information about a given student.
• list all the students in the course.
• display some statistics about the students enrolled in the course.
The applications should start by displaying a menu that has the flowing options:
1. Input a new roster.
2. Display the information of a student.
3. List all students registered in the class.
4. Display statistics.
5. Exit.

The first option should:
o ask the user to enter the total number of students registered in the class
o ask the user to enter the information for the students one student at a
time. It should do the following for each student until the information
for all the students is entered:
create a Student object and automatically assign a new student
ID to the new student. (Use simple serial numbering sequence
that starts with 1 and is incremented every time a new student
is added.)
ask the user to input the following information for the student:
• student name
• street address
• city
• state
• ZIP
• GPA
o After the user inputs the above fields for one student, the program
should loop back to ask for the information of the next student until all
students have been entered in the roster.

The second option should:
o ask for the student ID then searches the roster for that student.
o If it is found then student information’s is displayed otherwise a
message should be displayed to indicate that the student is not
registered in the course.

The third option should list all the students in the roster. Display only the ID,
name, ZIP and the GPA.

The forth option will calculate and display the following:
o The average GPA of the students in the course.
o The lowest GPA.
o The highest GPA.

The fifth option ends the application.
Programming Guidelines:

The program should use dynamic memory allocation to create the roster.

Use the switch statement to implement the required menu.

Each menu option (other than the exit option) should be implemented as an
individual function.

When the user selects an option from the menu (other than the exit option), the
program should perform the selected task then display the menu again.

The program should end ONLY if the user selects the “Exit” option.

Make sure your program doesn’t have any memory leaks by releasing all the
dynamically created memory when it is no longer needed.

Submit only the source code of your program:
The header file for the Address class (Address.h)
o
The header file of the Student class (Student.h)
o
The implementation file for the Address class (Address.cpp)
o
o
The implementation file of the Student class (Student.cpp)
o
The source file for the application (Roster.cpp)


Here is my code so far...
STUDENT.H

#include <iostream>
using namespace std;

class Student
{
	private:
		char studentName[30];
		int studentID; ///Figure this one out
		//char studentAddress streetAddress();
		float GPA;
	public:
		char getName();
		int getID();
		char getStreet();
		float getGPA();
	
		void setName(char*);
		void setID(int);
		char setStreet();
		void setGPA(float);

		//friend class Address; // friend of class Address
};

STUDENT.CPP

#include "Student.h"
using namespace std;

char Student::getName()
{
	return studentName[30];
}

int Student::getID()
{
	return studentID;
}

/*string Student::getStreet()
{
	return 0;//just a test
}*/

float Student::getGPA()
{
	return GPA;
}

void Student::setName(char *myName)
{
	strcpy_s(studentName, myName);
}

void Student::setID(int ID)
{
	for (int i = 0; i < ID; i++)
	{
		studentID = 1;
		studentID++;
	}
}

/*Student::setAddress()
{
	return 0;//just a test
}*/

void Student::setGPA(float myGPA)
{
	GPA = myGPA;
}

ADDRESS.H

#include <iostream>
using namespace std;

class Address
{
private:
	char streetAddress[50];
	char city[30];
	char state[30];
	int zip;
public:
	char getStreet();
	char getCity();
	char getState();
	int getZip();

	void setStreet(char*);
	void setCity(char*);
	void setState(char*);
	void setZip(int);

	friend class Student;
};

ADDRESS.CCP

#include "Address.h"
using namespace std;

char Address::getStreet()
{
	return streetAddress[50];
}

char Address::getCity()
{
	return city[30];
}

char Address::getState()
{
	return state[30];
}

int Address::getZip()
{
	return zip;
}


void Address::setStreet(char * myStreet)
{
	strcpy_s(streetAddress, myStreet);
}


void Address::setCity(char * myCity)
{
	strcpy_s(city, myCity);
}


void Address::setState(char * myState)
{
	strcpy_s(state, myState);
}

void Address::setZip(int myZip)
{
	zip = myZip;
}

ROSTERAPP.CPP

//Course Roster Application
#include <iostream>
#include "Address.h"
#include "Student.h"
using namespace std;

int menu();
void enterNew(), displayStudent(), listStudents(), dispStats();

int main()
{
	int choice;

	do {
		choice = menu(); //get selection
		switch(choice) {
			case 1: enterNew();
				break;
			case 2: displayStudent();
				break;
			case 3: listStudents();
				break;
			case 4: dispStats();
				break;
			case 5: break;
			default: cout<<"Please try again.\n\n";
		}
	} while(choice !=5);

	return 0; 
}


int menu() //Menu which returns a user's selection.
{
	int choice;

	cout << "1. Input a new roster.\n";
	cout << "2. Display the information of a student.\n";
	cout << "3. List all students registered in the class.\n";
	cout << "4. Display statistics.\n";
	cout << "5. QUIT.\n";
	cout << "\nChoose one: ";
	cin >> choice;

	return choice;
}

/*
int* a = NULL;   // Pointer to int, initialize to nothing.
int n;           // Size needed for array
cin >> n;        // Read in the size
a = new int[n];  // Allocate n ints and save ptr in a.
for (int i=0; i<n; i++) {
    a[i] = 0;    // Initialize all elements to zero.
}

*/
void enterNew()
{
	system("CLS");
	int n;
	cout << "Please enter the total number of students registered in the class: ";
	cin >> n;
	cout << "\nThanks!\n";
	int *arr = new int[n];

	Student *stu;
	stu = new Student[n];
	char *name;
	name = new char[30];
	cout << "\nPlease enter the information one student at a time... ";
	
	//stu[0].setID(n);
	for (int i = 0; i < n ; i++)
	{
		cout << "\nPlease enter the Student's Name: ";
		cin >> name;
		stu[i].setName(name);
	}

	for (int j = 0; j < n ; j++)
		cout << "NAME: " << stu[j].getName() << endl; //THIS IS ALL WRONG...

	delete [] arr;
	delete [] stu;
	arr = NULL;
	stu = NULL;
}

void displayStudent()
{
	cout << "This will display student info.\n\n";
}

void listStudents()
{
	cout << "This will list all students in a class.\n\n";
}

void dispStats()
{
	cout << "This will display average GPA, lowest GPA, and highest GPA.\n\n";
}

I have lots of commented stuff in there just to use as possible examples and several of which that don't work that I haven't gotten to, but as this is 35% of my grade and due in less than a week, I've gotten desperate... but not desperate enough to pay someone else to do my work! hahaha

What I was working on last was getting a user's input for a student's name, but it's just printing out "=" and so I've given up trying this by myself for the day...

Any help is welcome. Thanks!

Well, it certainly looks like you are on the right track.. well stated question and lots of code which shows effort on your part.

You've created all the necessary objects with the required attributes and functions. You've created the dynamic arrays of these objects as per the requirement. Your menu makes effective use of the switch/case structure; each case calling a specific function. Your code is self documenting and easy to follow. You effectively use the 'seperate compilation' by segregating your code into various header files and .cpp function definitions.

Let us know if you have any specific problems. I looked at your code for setting the student's name and didn't see any problems, maybe someone else will see it. (don't have a compiler on me' old laptop so I can't debug)

You are doing a lot of things right here.. have you taken a programming class before.??

Thanks.

The problem with the student name comes when I execute the RosterApp.cpp file. Instead of displaying the names I entered, it displays "=" as the name. I'm not so sure as to why, but my brain hurts because I've been staring at this all day today.

I took a C++ class a year ago (not my favorite language by the way), but for whatever reason wasn't able to take the final, so I got an "I." To clear this "I" and earn a grade, I have to do this project and take a comprehensive final, but I've moved on from C++ and so I'm struggling again.

I suppose problem with student's name is here:
student.cpp line 6

return studentName[30];

while studentName is defined as:

char studentName[30];

You return 30th element of studentName array while there are only elements in range 0-29. What's more You need to return whole string not just one char. Same problem occurs when You try to return adress, state etc.

Okay... so I have the program working and displaying everything correctly (mostly)... I only have 3 real problems now:

- BOTH HEADER FILES AND RELATED FUNCTIONS: I changed over my char arrays to strings (the project calls for maximums of characters). How do I easily change these over?
- STUDENT.H AND ROSTERAPP.CPP: Instead of using the "studentAddress: an object of the Address class" I created an Address object (stu1) and did it that way. I know it involves friends of classes/functions but I don't know how or where to begin.
- ROSTERAPP.CPP: in displayStudent I have a comment for the loop if the entered number is found, but sometimes I get a FC. What am I doing wrong?


If I can't figure these things out, I will probably turn this in as is, albeit for probably a lower grade, so I'm hoping for the best! Thanks in advance!

STUDENT.H

#include <iostream>
#include <string>
using namespace std;

class Student
{
	private:
		string studentName;
		int studentID;
		//string studentAddress;
		float GPA;
	public:
		string getName();
		int getID();
		string getStreet();
		float getGPA();
	
		void setName(string);
		void setID(int);
		void setStreet(string);
		void setGPA(float);

		//friend class Address; // friend of class Address
};

STUDENT.CPP

#include <string>
#include "Student.h"
using namespace std;

string Student::getName()
{
	return studentName;
}

int Student::getID()
{
	return studentID;
}

string Student::getStreet()
{
	return 0; //FIX
}

float Student::getGPA()
{
	return GPA;
}

void Student::setName(string myName)
{
	studentName = myName;
}

void Student::setID(int ID)
{
	ID = ID+1;
	studentID = ID;
}

void Student::setStreet(string myAddress)
{
	myAddress = myAddress;//FIX
}

void Student::setGPA(float myGPA)
{
	GPA = myGPA;
}

ADDRESS.H

#include <iostream>
#include <string>
using namespace std;

class Address
{
private:
	string streetAddress;
	string city;
	string state;
	int zip;
public:
	string getStreet();
	string getCity();
	string getState();
	int getZip();

	void setStreet(string);
	void setCity(string);
	void setState(string);
	void setZip(int);

	//friend string studentAddress();
};

ADDRESS.CPP

#include <string>
#include "Address.h"
using namespace std;

string Address::getStreet()
{
	return streetAddress;
}

string Address::getCity()
{
	return city;
}

string Address::getState()
{
	return state;
}

int Address::getZip()
{
	return zip;
}


void Address::setStreet(string myStreet)
{
	streetAddress = myStreet;
}


void Address::setCity(string myCity)
{
	city = myCity;
}


void Address::setState(string myState)
{
	state = myState;
}

void Address::setZip(int myZip)
{
	zip = myZip;
}

ROSTERAPP.CPP

//Course Roster Application
#include <iostream>
#include <string>
#include "Address.h"
#include "Student.h"
using namespace std;

Student *stu;
Address *stu1;
int num;
int n;
int *arr = new int[n];
string name, address, city, state;
int zip;
float gpa;

int menu() //Menu which returns a user's selection.
{
	int choice;

	cout << "1. Input a new roster.\n";
	cout << "2. Display the information of a student.\n";
	cout << "3. List all students registered in the class.\n";
	cout << "4. Display statistics.\n";
	cout << "5. QUIT.\n";
	cout << "\nChoose one: ";
	cin >> choice;

	return choice;
}

void enterNew()
{
	system("CLS");

	cout << "Please enter the total number of students registered in the class: ";
	cin >> n;
	num=n;
		
	stu = new Student[n];
	stu1 = new Address[n];
	
	cout << "\nPlease enter the information one student at a time... \n";
	
	for (int i = 0; i < n ; i++)
	{
		stu[i].setID(i);
		cout << "\nStudent ID set to: " << stu[i].getID();

		cout << "\nPlease enter the Student's Name: ";
		cin >> name;
		stu[i].setName(name);

		cout << "Please enter the Student's Address: ";
		getline(cin,address, '.');
		stu1[i].setStreet(address);

		cout << "Please enter the Student's City: ";
		cin >> city;
		stu1[i].setCity(city);

		cout << "Please enter the Student's State: ";
		cin >> state;
		stu1[i].setState(state);

		cout << "Please enter the Student's ZIP: ";
		cin >> zip;
		stu1[i].setZip(zip);

		cout << "Please enter the Student's GPA: ";
		cin >> gpa;
		stu[i].setGPA(gpa);
	}

	for (int j = 0; j < n ; j++)
	{
		cout << "\nID: " << stu[j].getID();
		cout << "\nNAME: " << stu[j].getName();
		cout << stu1[j].getStreet() << endl;
		cout << stu1[j].getCity() << ", " << stu1[j].getState() << " " << stu1[j].getZip() << endl;
		cout << stu[j].getGPA() << endl;
	}

}

void displayStudent()
{
	system("CLS");
	int id=0;

	cout << "Please enter the Student ID for the student: ";
	cin >> id;
	if (stu[id].getID() == false) //WORKING, but not the way it should... causes an FC error
		cout << "The student is not registered in the course!\n\n";
	else 
	{
		cout << "\nNAME: " << stu[id-1].getName() << endl;
		cout << "ID: " << stu[id-1].getID();
		cout << stu1[id-1].getStreet() << endl;
		cout << stu1[id-1].getCity() << ", " << stu1[id-1].getState() << " " << stu1[id-1].getZip() << endl;
		cout << stu[id-1].getGPA() << endl << endl;
	}

	
}

void listStudents()
{
	system("CLS");
	for (int j = 0; j < num ; j++)
	{
		cout << "ID: " << stu[j].getID();
		cout << "\nNAME: " << stu[j].getName() << endl;
		cout << stu1[j].getZip() << endl;
		cout << stu[j].getGPA() << endl << endl;
	}
}

void dispStats()
{
	system("CLS");
	float average = 0, sum = 0, high = 0, low = 0;

	for (int j = 0; j < num; j++) // Gets the sum of all the GPA's
	{
		sum += stu[j].getGPA();
	}
	average = sum/num; // Calculates average

	for (int i = 0; i < num; i++) //Calcultes highest and lowest GPA's
	{
		if(high < stu[i].getGPA())
		{
			high = stu[i].getGPA();
		}
		else
			low = stu[i].getGPA();
	}

	cout << "The average GPA of the students in the course is: " << average << endl;
	cout << "The lowest GPA is: " << low << endl;
	cout << "The highest GPA is: " << high << endl;
}

int main()
{
	int choice;

	do {
		choice = menu(); //get selection
		switch(choice) {
			case 1: enterNew();
				break;
			case 2: displayStudent();
				break;
			case 3: listStudents();
				break;
			case 4: dispStats();
				break;
			case 5: break;
			default: cout<<"Please try again.\n\n";
		}
	} while(choice !=5);

	delete [] arr; //Delete all that stuff! 
	delete [] stu;
	delete [] stu1;
	arr = NULL;
	stu = NULL;
	stu1 = NULL;
	return 0; 
}

- BOTH HEADER FILES AND RELATED FUNCTIONS: I changed over my char arrays to strings (the project calls for maximums of characters). How do I easily change these over?

This doesn't seem like it would be that big of a deal. Change the header from <cstring> to <string>. Anytime you want to compare string objects use == instead of strcmp(), anytime you want to assign stuff to your string, use the assignment operator = instead of strcpy(), anytime you want to add to a string, use += instead of strcat(), etc, etc.

- ROSTERAPP.CPP: in displayStudent I have a comment for the loop if the entered number is found, but sometimes I get a FC. What am I doing wrong?

The success of this test depends on getID() to return non-zero "TRUE" if studentID exists, or return 0 "FALSE" if studentID has not been populated:

if (stu[id].getID() == false) //WORKING, but not the way it should... causes an FC error

As of right now, whenever a 'Student' object is created, none of the private variables are ever initialized.. so even if you create a new 'student' object, there is no guarantee that studentID will be zero (false), even though it has not been assigned anything. To make sure studentID is zero whenever new 'student' objects are created, we will make a simple default constructor. This is a function that will automatically get called every time a new 'student' object is created. We can use this feature to initialize all our class variables:

class Student
{
     private:
   
     string studentName;
     int studentID;
     //string studentAddress;
     float GPA;

     public:

     //Default Constructor
     Student();

     string getName(); 
     int getID();
     string getStreet();
     float getGPA();
     void setName(string);
     void setID(int);
     void setStreet(string);
     void setGPA(float);

//friend class Address; // friend of class Address

};

Function Definition:

Student::Student()
{
     studentID = 0;
     GPA = 0.0;
}

Now you can do something like this, and know that your class variables are all properly initialized to zero:

//Default constructor is called here...
//100 student objects all properly initialized
Student myStudents[100];

Now you can test your objects with certainty, if they haven't been assigned anything, they will return FALSE.

Thanks! Okay so that takes care of my rosterapp.cpp problem, but that leaves the first and last one you replied to.

Re: The first suggestion about the header files and related functions - I'm sorry but I tend to need an example to look at to understand what you mean. I don't need the answer, but if I can see what's going on in the problem, then I can adjust and fit to my own needs. I currently have strings (studentName, streetAddress, city, state) that don't have a maximum # of characters. The paper tells me I need "a string of a maximum of XX characters" (XX is 50 in some cases, 30 in others). I know I can do this by removing string and changing over to char studentName[30], but then I have to change over many related functions because you can't copy over an array using "=". This is what I know. After that I overthink and confuse myself.

Re: The last suggestion you made of making a Student object with 100 objects. This cannot be done as the user has to tell me how many students are on the roster. I have already have this created dynamically so that can be taken care of. Your suggestion of the default constructor did work for me though. This is the type of simple stuff I overlook.

And then I still have the problem of the STUDENT.H AND ROSTERAPP.CPP... I just don't know where to begin.

Thanks for all your help so far!

Re: The last suggestion you made of making a Student object with 100 objects. This cannot be done as the user has to tell me how many students are on the roster. I have already have this created dynamically so that can be taken care of. Your suggestion of the default constructor did work for me though. This is the type of simple stuff I overlook.

I am aware of this.. I was just trying to demonstrate how you could create a bunch of objects of a certain class and know that all those objects have been initialized via the class default contructor.

Oh okay. I see your point now. Sorry for the confusion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.