Program that has student name, id number, and classes registered

closed account (ozTkSL3A)
Hey guys, I need to write a class named student that gets the
1. student name
2. integer id number
3. classes registered- using array of strings
I'm supposed to use constructors for this class, and write get and set functions, and also in main, create two instances of students. My current code only gets the student name and id number, not the classes registered. It is also just one instance of a student.
Any help is greatly appreciated.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstdlib>
using namespace std;

class Student
{
private:
	int ID;
	string name;
	
public:
	Student();
	Student(int ID, string name);
	void setStudent(int ID, string name);
	int getID();
	string getName();
	void print();
};
Student::Student()
{
	ID = 0;
	name = "";
}
Student::Student(int ID, string name)
{
	this->ID = ID;
	this->name = name;
}

void Student::setStudent(int ID, string name)
{
	this->ID = ID;
	this->name = name;
}

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

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


void Student::print()
{
	cout << "ID : " << ID << endl;
	cout << "Name : " << name << endl;
}
int main()
{
	Student s;
	int ID;
	string name;

	cout << "Enter ID ";
	cin >> ID;
	cout << "Enter name ";
	cin >> name;
	s.setStudent(ID, name);
	s.print();
	return 0;
}
/* I was thinking this added somewhere would do the part for classes registered, but it doesn't and just crashes the program. 
int const size = 1000;
	int i = 0;
	char x[size];
	cout << "enter your courses";
	cin.getline(x, size);
	while (x[i] != '/0') {
		cout << x[i];
			i++;
	}
	cout << endl;
*/
The commented code will get you entire line.
For example if user enters "geo phy chem" on command line then x will contain all three words/strings. What you need is array of "char *" (or std::string if you are allowed to do that)

for crashing, try changing line 75 to char x[size]={0};
You need to add an array of strings as a private member field. Give the array a maximum capacity (lets says 1000) and start with a size of 0.

I would have an addClass() function that adds a class to the end of the array (while also incrementing size accordingly), and a printClass() function which prints the entire array.
Last edited on
closed account (ozTkSL3A)
Hey guys, actually I was supposed to just code in the values for the courses registered. There was no need to prompt the user. How would I do this then?
So the arrays values will be hard-coded? In that case, simply make the array as a data field and fill it in with registered classes.

Can we see the full assignment instructions?
Last edited on
closed account (ozTkSL3A)
Thanks guys, I ended up getting it.
Topic archived. No new replies allowed.