vector

Dec 31, 2010 at 4:01am
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class student
{
    protected:
    vector<string> subject;
    string facultly;
    string name;
    public:
    student(string name, string facultly,vector<string> subject)
    {
        this->name=name;
        this->facultly = facultly;
        this->subject=subject;
    }
    void setname(string name)
    {
        this->name=name;
    }
    void setfacultly(string facultly)
    {
        this->facultly = facultly;
    }
    void setsubject(string const& d)
    {
        subject.push_back(d);
    }
    vector<string> getsubject()
    {
        return subject;
    }
    string getname()
    {
        return name;
    }
    string getfacultly()
    {
        return facultly;
    }
};

int main()
{
    string subjects;
    string name;
    string facultly;
    vector<string>subject;
    vector<student>stud;

    getline(cin,name);
    student[0].setname(name);
    stud.push_back(name);

    cin>>facultly;
    student[0].setfacultly(facultly);
    stud.push_back(facultly);

    char input='y';
     while(input=='y' || input=='Y')
    {
    cout<<"Subject name=";
    cin>>subjects;
    student[0].setsubject(subjects);
    subject.push_back(subjects);
    cout<<"Do you want to register more students?(Y or N)"<<endl;
    cin>>input;
    }
    student[0].setsubject(subject);
    stud.push_back(subject);
}


can someone tell me where i got error?i got error, cant compile.
Dec 31, 2010 at 4:05am
You got an error at every instance of student[0]. What were you trying to do there?

Additionally, you got errors when you tried to push_back() individual elements of students, because you're supposed (for stud) to push_back() students. I'd suggest you create a temporary variable that you store your data in, and at the end of the loop push_back() your students.

-Albatross
Last edited on Dec 31, 2010 at 4:07am
Dec 31, 2010 at 4:24am
because the class student will be storing many student record,so there will be student[0],student[1],student[2].....

i m trying to do vector inside vector,inside a container of vector, got student name, facultly and subjects
and subjects itself is a vector also.because a student can add more than one subjects.

Dec 31, 2010 at 11:15am
i didn't understand student[0], 1, 2. you first must set name of stud[0].setname(), then you don't need to write stud.push_back(name), this an error. when you setname of stud[0], name goes to that student. anyway you can not write student[0].
Dec 31, 2010 at 1:15pm
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class student
{
    protected:
    string subject;
    string facultly;
    string name;
    public:
    student(string name, string facultly,string subject)
    {
        this->name=name;
        this->facultly = facultly;
        this->subject=subject;
    }
    void setname(string name)
    {
        this->name=name;
    }
    void setfacultly(string facultly)
    {
        this->facultly = facultly;
    }
    void setsubject(string subject)//const& d)
    {
        this->subject = subject;//subject.push_back(d);
    }
    string getsubject()
    {
        return subject;
    }
    string getname()
    {
        return name;
    }
    string getfacultly()
    {
        return facultly;
    }
};

int main()
{
    string subjects;
    string name;
    string facultly;
    vector<stud>subject;
    vector<student>stud;

    getline(cin,name);
    stud[0].setname(name);
    stud.push_back(stud[0]);

    cin>>facultly;
    a.setfacultly(facultly);
    //stud.push_back(facultly);

    char input='y';
     while(input=='y' || input=='Y')
    {
    cout<<"Subject name=";
    cin>>subjects;
    subject[0].setsubject(subjects);
    subject.push_back(subjects);
    cout<<"Do you want to register more students?(Y or N)"<<endl;
    cin>>input;
    }
    stud[0].push_back(subject);
    stud.push_back(subject);
}


i change to this code, aso got error, can anyone explain to me why?
Dec 31, 2010 at 1:24pm
Could you tell the error?
Edit: it seems that you don't know the difference between class and instance
Last edited on Dec 31, 2010 at 1:26pm
Jan 2, 2011 at 3:00am
instance is the object of the class,am i right?
Jan 2, 2011 at 3:13am
1
2
3
class student;
vector<stud> subject; //what is a stud?
vector<student> stud; //a class or an object? 

Also line 67:subject[0].setsubject(subjects); At this point subject is empty, so you can't access subject[0] (try with vector::at instead of operator[], if fails it throws an exception)
subject.push_back(subjects); You lose me with the names, but I think that subjects is a string and it can't be converted to a stud.
Jan 2, 2011 at 11:34am
thanks for all reply. I had figure out my way to do it:)
Jan 9, 2011 at 3:41am
yts, can u put ur code at here for me a reference? coz I doing the similar question with u,Thank you, Pls upload it, I'm in urgent case
Jan 9, 2011 at 4:12am
kingdof,

Wouldn't that ruin the purpose of doing the homework, which is to improve your skills with the language?

-Albatross
Topic archived. No new replies allowed.