Need some help with public functions/class for a "gradebook"

I am trying to write a program that stores a list of class objects named GradeBook by using vector, and allows users to add, remove or sort objects in the list by interactively giving commands until the user enters quit. The class GradeBook should contain three private data members: int StudentID, string StudentName, and double Grade (from 0.0 100.0).

Access to these private data members should be implemented through public member functions i.e. set member functions and get member functions.

My current code has been giving me trouble with some errors such as:
'void StudentRecord::add()' is private
and
" 'class StudentRecord' has no member named 'remove'.
" 'class StudentRecord' has no member named 'sort'.
" 'class StudentRecord' has no member named 'print'.

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
 #include <vector>
#include <algorithm>
#include <functional>
#include <iostream>


using namespace std;

class StudentRecord{

int id ;
char sname[20] ;
double grade ;

void add(void){
vector <int> v1; // vector container
vector <int>::iterator Iter1; // iterator
int i, n;
printf("Enter the total number of record"); // total records of a students
cin >> n; // push student data in range , id starts from 0

for (i = 0; i <= n; i++){
    v1.push_back(i);
}

void remove (){
cout << "Enter the student ID to remove" << endl;
cin >> id1;
for (i = 0; i <= n; i++){
    if ( std::find(v1.begin(), v1.end(), id1) != v1.end() )
        v1.erase (id1);
    else
        cout<< "Student not found" << endl;
}
}

void sort (){
sort(v1.begin(), v1.end()); // do the sorting according to the student id
cout<<"\nSorted v1 vector is: ";
for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
    cout<<*Iter1<<" ";
    cout<<endl;

sort(v1.begin(), v1.end(), greater<int>()); // to sort in descending order
cout<<"\nRe-sorted (greater than) v1 vector is: ";
for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
    cout<<*Iter1<<" ";
    cout<<endl;
}

void print(){
cout << "Original v1 vector is: "; // print the data
for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
}
}
}; // end of class definition

int main(void){

int choice;
StudentRecord s1 ; // creating object
s1.add();
s1.remove();
s1.sort();
s1.print();

return 0;

}


Any type of help is appreciated! Thank you.
Last edited on
Bump.
Stick public: on line 8. This will make every class function and class member reachable from outside the class. It's awful design and dangerous but will fix your immediate problem.

Your function remove appears to be inside your function add. This is because your code layout is awful and you have made it hard for you to see what you're doing. Indentation exists for a reason.

This
1
2
3
for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
    cout<<*Iter1<<" ";
    cout<<endl;

is the same as this:
1
2
3
4
5
for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
{
    cout<<*Iter1<<" ";
}
cout<<endl;

Did you mean for the cout<<endl; to be after the loop, or inside it? Use braces.
Last edited on
Moschops,

I did added Public: and added braces for my for loops, and now I am getting the errors:
" 'StudentRecord' was not declared in this scope"
" Expected ';' before 's1' "
" 's1' was not declared in this scope "


Any idea on how to make my main function at the bottom work properly with the other functions inside? Thanks.
Last edited on
line 16: v1 is a local variable inside add(). When add exits, v1 goes out of scope.

line 30,38,40,44,46,53: Where does v1 exists?

Line 19: Why the printf? If you're writing C++, use cout.

add(), remove(), sort() should not be members of StudentRecord. add(), remove(), sort() all refer to the collection of student records.
Last edited on
@AbstractionAnon

Is there any way you could try to compile my code and see how to adjust it? I can make v1 an int to declare it but I don't think the functions in my int main() work.

Would really appreciate more help.
I'm not going to do your assignment for you.

A few additional comments.

- Per the instructions, the data members are to be private. Therefore, public should be at line 14, not at line 8 as Moschops suggested.

- The instructions say the class is to be named GradeBook. Is there a reason you named it StudentReord?

- Move line 16 to line 61. vector<int> v1 belongs in main().

- Move add(), remove(), sort(), print() out of the StudentRecord class. As noted previously, they should not be member functions. They should be stand-alone functions. You're going to need to pass v1 to each of those functions by reference.

If I were doing this, I would create a collection class that contains the vector v1. add(), remove(), sort(), and print() would then be members of the collection class since they all deal with a collection of student records.








Topic archived. No new replies allowed.