Arrays in classes!

Apr 2, 2013 at 3:43pm
Hey all, I need help with a small problem... I have to declare a class called Module. And in the main function I have to declare an array of type Module. Then I have to ask the user to type in the 5 modules they are registered for and the marks for assignment 1 and assignment 2 for each module... I don't know how to "save" the input from the user into the array and then the class? After that I have to determine the semester mark, but that is not difficult, I just need help with the input. This is what I have so far:

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

class Module
{
public:
    void setMods(string moduleX, int ass1, int ass2);
    string get_module();
    int get_ass1marks();
    int get_ass2marks();
private:
    string module;
    int ass1marks;
    int ass2marks;
};

int main()
{
    Module mods_ass[5];
    string moduleName;
    int assOne, assTwo;

    for (int i = 1; i <= 5; i++)
    {
        cout << "Enter the name of module " << i << ": ";
        cin >> //here I had mods_ass[i].module the the error says:
               //error: 'std::string Module::module' is private
               //error: within this context
        cout << "Enter the marks for Assignment 1" << ": ";
        cin >> //the same goes here
        cout << "Enter the marks for Assignment 2" << ": ";
        cin >> //the same goes here
    }

    return 0;
}

void Module::setMods(string moduleX, int ass1, int ass2)
{
    module = moduleX;
    ass1marks = ass1;
    ass2marks = ass2;
}

string Module::get_module()
{
    return module;
}

int Module::get_ass1marks()
{
    return ass1marks;
}

int Module::get_ass2marks()
{
    return ass2marks;
}


I tried different variables, but nothing works!
Apr 2, 2013 at 3:46pm
You will need to get the information into different variables then use your setMods() function to fill in the values in your class.

Apr 2, 2013 at 3:53pm
OK let me go and try that!
Apr 2, 2013 at 6:59pm
I beleive your array needs to be declared in the private section of your class...correct me if im wrong im learning still :)
Apr 2, 2013 at 7:08pm
@Michael Kane
I thought so 2! But my question says "Write a main program that does the following: (1)declare an array..." etc :)
Topic archived. No new replies allowed.