Vector of Structures with a vector

First I would like to say this is a homework assignment. I am by no means asking for help with it directly. I am getting weird output on the vector inside the structure and I am curious as to why. Everything else stores and prints just fine. I have had 2 instructors not figure it out so far and for 3 days I've been lost. Any help is appreciated, thank you. The problem is when the students vector prints out the students it acts as if it is printing the previous elements with the next element to be printed. The output should just list things about the class and the names of the students in the class. What happens with what I have is that it prints the first set of names entered multiple times then at some point it prints the second set and the first set. When those are done the last set finally prints 1 time. Im lost

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <algorithm>


using namespace std;

/*classroom structure*/

struct classroom{
    int roomNumber;
    string lectureName;
    vector<string> students;
    int numOfChairs;
    string window;
    string projector;
    string available;
};

/*function prototypes*/

int setRoomNum(classroom &clas);
string setLectName(classroom &clas);
void setStudents(classroom &clas);
int setNumChairs(classroom &clas);
string setWindow(classroom &clas);
string setProjector(classroom &clas);
string setAvailable(classroom &clas);
classroom setClass(classroom &clas);
void printDetails(vector<classroom> &vec);


vector<classroom> classRm; // global vector holding structures.

int main(){


    classroom cls; // initialize an object of type classroom. This will pass structures into my vector

        for(int i=0;i<3;i++){
            classRm.push_back(setClass(cls)); // filling vector with structures, setClass gets info for each structure

        }

    printDetails(classRm); // problem area, seems in the nested for loop


    return 0;
}
int setRoomNum(classroom &clas){
    cout << "Please Enter the Room Number: ";
    cin >> clas.roomNumber;
    cout << endl;
    return clas.roomNumber;
}
string setLectName(classroom &clas){
    cout << "Please Enter the Lecture Name: ";
    cin >> clas.lectureName;
    cout << endl;
    return clas.lectureName;
}
void setStudents(classroom &clas){
    string name;
    cout << "Please Enter Student Names: " << endl;
    cout << "If No More Students to add, enter 'none'" << endl;
        while (getline(cin, name) && name !="none"){ // the problem could be on this line as well
            clas.students.push_back(name);
        }
}
int setNumChairs(classroom &clas){
    cout << "Please Enter the Number of Chairs: ";
    cin >> clas.numOfChairs;
    cout << endl;
    return clas.numOfChairs;
}
string setWindow(classroom &clas){
    string answer;
    cout << "Does the Class Have Windows: ";
    cin >> answer;
    if (toupper(answer[0])=='Y'){
        clas.window="Yes";
    }
    else{
        clas.window="No";
    }
    return clas.window;
}
string setProjector(classroom &clas){
    string answer;
    cout << "Does the Class Have a Projector: ";
    cin >> answer;
        if (toupper(answer[0])=='Y'){
            clas.projector="Yes";
        }
        else{
            clas.projector="No";
        }
    return clas.projector;
}
string setAvailable(classroom &clas){
    string answer;
    cout << "Is the Classroom Available: ";
    cin >> answer;
        if (toupper(answer[0])=='Y'){
            clas.available="Yes";
        }
        else{
            clas.available="No";
        }
    return clas.available;
}

void printDetails(vector<classroom> &vec){

    for(int i=1;i<vec.size();i++){
        cout << vec[i].roomNumber << "\n";
        cout << vec[i].lectureName << "\n";
        cout << vec[i].numOfChairs << "\n";
        cout << vec[i].window << "\n";
        cout << vec[i].projector << "\n";
        cout << vec[i].available << "\n";
            for (int j=0;j<vec[i].students.size();j++){ // my problem is in here somewhere
               cout << vec[i].students[j] << endl;
            }

    }

}


classroom setClass(classroom &clas){

    setRoomNum(clas);
    setLectName(clas);
    setStudents(clas);
    setNumChairs(clas);
    setWindow(clas);
    setProjector(clas);
    setAvailable(clas);
    return clas;

}
1
2
3
4
5
6
    classroom cls;

        for(int i=0;i<3;i++){
            classRm.push_back(setClass(cls)); 

        }
You have one classroom object and then iterate over it.

The following is untested, but I think what you want is:
1
2
3
4
5
6
    classroom cls[3];

        for(int i=0;i<3;i++){
            classRm.push_back(setClass(cls[i])); 

        }


Also line 117 should be for(int i=0;i<vec.size();i++){
I was thinking that cls could be used as a temporary place holder before dumping the structure into say element 0 then the next iteration of the loop would overwrite what was stored in that object and then it would be dumped into element 1 and so on.
Edit: also on line 117 that was a mistake, it was originally 0 but when I used 1 the results looked a little better but it set the rest off of course by 1. it was just all weird
Last edited on
I was thinking that cls could be used as a temporary place holder

Makes sense and works. Sorry about that. I was just eyeballing your code and didn't get into it that deep.

Your code seems to work fine here if I add the following to setStudents() before the while loop: clas.students.clear();
I should have tried the .clear before posting to be honest. I thought about it but never tried to implement it, after the instructors couldn't figure it out I guess I was just plain ready to turn to the community. Both of the options do work, now to see which I want to use. And of course my \t's in the formatting of the output im working on right now just do not want to work with me. stupid cmd window!!
Topic archived. No new replies allowed.