I'm trying to create a vector that has vectors inside it, but I'm dealing with string
and cin
My goal is to ask the user to list out student names, and for each student listed, a vector will be created under "stuNameVecs" vector. Then these sub-vectors should hold int
values (points received on exam) for each student, as I will later use each student's vector data to calculate the student's total grade in the class.
Example (What I need it to look like, I know that's not how you actually do it):
mainVector<string> allStudents (vector<int>brendanVec, vector<int>jackVec, vector<int> johnVec) // Elements created by .push_back
vector<int> brendanVec(1, 5, 7, 5, 4) // Elements created by .push_back
vector<int> jackVec(4, 2, 6, 9, 8) // Elements created by .push_back
vector<int> johnVec(3, 5, 6, 2, 3) // Elements created by .push_back
This is my code
int main()
{
vector<vector<string> > stuNameVecs // This vector is supposed to hold the vectors of each student
int nOfStudents = 3;
char stuName[10];
cout << "Okay, begin by listing out the student's names:" << endl;
for(int i=0;i<nOfStudents;i++){
cout << i+1 << ". ";
cin.getline(stuName, 10);
stuNameVecs.push_back(vector<string>); // This is where I'm getting confused. It's supposed to create a
// new vector with the student's name inside that main vector
}
Keep in mind I'm still a beginner to C++ and through all my research, I've only found examples of 2D vectors using int
(not string
) for data values, and for
loops to fill up the vectors (not .push_back
).
Any help is appreciated.