Getting a lot of errors with my first attempt at classes

closed account (jwC5fSEw)
I've been going through this forum just to pick up tips for beginners, and I found this topic: http://www.cplusplus.com/forum/beginner/19643/ While I realize it's bad to help someone who can't learn for himself, I wanted to try the problem for myself. I thought I'd try to use classes (which I've never used before) to make it smaller, and I came up with this:

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
#include <iostream>
#include <string>
using namespace std;

class Student {
	string name;
	int ID, mark;
 public:
	void set_values(string,int,int);
}

void Student::set_values(string a, int b, int c){
	name = a;
	ID = b;
	mark = c;
}

int main(){
	Student one, two, three;
	string name_input;
	int ID_input, mark_input;

	for (int n=0; n<3; n++) {
		cout << "Enter student information" << endl;
		cout << "\nName: ";
		getline(cin,name_input);
		cout << "\nID: ";
		getline(cin,ID_input);
		cout << "\nMark: ";
		getline(cin,mark_input);
		if (n == 0)
			one.set_values(name_input,ID_input,mark_input);
		if (n == 1)
			two.set_values(name_input,ID_input,mark_input);
		if (n == 2)
			three.set_values(name_input,ID_input,mark_input);
	}
	cout << one.name << " " << one.ID << " " << one.mark << " " << endl;
	cout << two.name << " " << two.ID << " " << two.mark << " " << endl;
	cout << three.name << " " << three.ID << " " << three.mark << " " << endl;
	cout << "The average mark is " << (one.mark + two.mark + three.mark)/3;
}


That gets a load of errors. Can anybody tell me why? I'll post the errors if requested.
It's helpful if you post the errors.

I did see offhand that you are accessing mark and the other members like they are public, but since they are private variables, they cannot be accessed like that. You have to use a member function or something similar to get them.
Last edited on
1) Classes must have a semicolon after their body:

1
2
3
4
class Student
{
  // stuff here
};  // <- don't forget this semicolon 


2) getline only works with strings. ID and mark are integers. If you want to get user input in the form of an int, you can do cin >> ID_input; although there are problems with mixing between >> and getline -- none of which I ever understood or cared about (I like never do console I/O crap)

3) on lines 38 and up, you can't access Student's members directly because they're private (members are private by default). Either

a) make them public:

1
2
3
4
5
class Student
{
public:
    string name;
    // etc 


or b) write a series of 'get' functions which return the members.

I'd recommend option a in this instance.


4) This is not an error, but learn about arrays and indeces so you don't have to do if chains like that. The whole idea behind arrays is that they work great in loops.

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
//Student one, two, three;  // blech
Student students[3];  // better

//...

/*  blech
if (n == 0)
  one.set_values(name_input,ID_input,mark_input);
if (n == 1)
  two.set_values(name_input,ID_input,mark_input);
if (n == 2)
  three.set_values(name_input,ID_input,mark_input);*/

students[n].set_values(name_input,ID_input,mark_input);  // better

// ...

/* blech
cout << one.name << " " << one.ID << " " << one.mark << " " << endl;
cout << two.name << " " << two.ID << " " << two.mark << " " << endl;
cout << three.name << " " << three.ID << " " << three.mark << " " << endl;
*/

for(int n = 0; n < 3; ++n)  // better
  cout << students[n].name << " " << students[n].ID << " " << students[n].mark << " " << endl;
closed account (jwC5fSEw)
EDIT: Posted this before I saw the above post. Made those changes and it compiled fine. Also, using getline did present some weird problems, so I switched it to cin >> name_input and it worked fine. And thanks for the array tips as well, I was hoping there'd be a way to make those lines less redundant!
Last edited on
Topic archived. No new replies allowed.