0

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.

Ahed N
  • 1
  • 1
    What method are you using to learn C++? There are a few basic mistakes you're making here and it's hard to know where to start. If you have a book or something we might be able to direct you to the correct chapter. – JohnFilleau Mar 21 '20 at 02:40
  • Barring an answer on that - your compiler is a good source of information about how code is supposed to look. The errors they spit out can be pretty arcane, but it's essential to learn how to read them, and I promise they'll stop looking like hieroglyphics eventually. What error message is your compiler giving you? Start with that one. See if you can find an answer using google and stackoverflow, and if not ask a question about that one specific error. – JohnFilleau Mar 21 '20 at 02:43
  • If you want to store the names and ints I suggest to use `std::map>`. Because If u want to store names and ints `std::vector>` and `std::vector>` are not valid and using `std::vector>` is useless – asmmo Mar 21 '20 at 02:45
  • @John What do you mean? The "Example" code is obviously not real C++, but does my actual code have problems? I know the very last line is incorrect because that's where I'm looking to get help on, but besides that, can you point out where? – Ahed N Mar 21 '20 at 03:03
  • Sure - missing a semicolon on your first line, which seems like a nitpick, but combined with everything else it seems relevant, using `char stuName[10]` to hold the input name when you have access to the `std::string` class instead, " for each student listed, a vector will be created under "stuNameVecs" vector" which makes me think you don't know what a `std::string` is or you don't know what a `std::vector` is. – JohnFilleau Mar 21 '20 at 03:08
  • A `std::string` is an alias for `std::basic_string` which is a container that holds a sequence of `char`s. It makes more sense to store each name in a `std::string`, not in a `std::vector`, unless you were to want to split each name on white space and store each of those tokens in its own string. But from the code it looks like you just want a `std::vector`. – JohnFilleau Mar 21 '20 at 03:10
  • Under the section "_This is my code_" it would help if you placed code that compiled. – Ted Lyngmo Mar 21 '20 at 03:10
  • I *highly recommend* learning C++ from a good book on the subject. It's such a general purpose language, widely known and regarded as a ticket to a job, that there's no shortage of tutorial sites with less-than-stellar examples and curriculum. Like low-effort click-bait "news" websites, except for C++ examples. – JohnFilleau Mar 21 '20 at 03:14
  • @John Oh I see, I used `char` instead of `string` because I had to use `cin.getLine`, and that wasn't working with `string`. I couldn't figure out how to use it with `string`, if there is a way, it'd be great to know how. I see how what I'm saying is confusing, I know that a vector is basically a dynamic array that holds values. I want my code to generate new vectors named after each student, holding `int` values in each vector (inputted by the user). Sorry if my wording was confusing – Ahed N Mar 21 '20 at 20:17
  • On a code that compiles, this doesn't compile because I don't know how to let my code use `cin.getLine` to `.push_back` values in a vector inside of another vector. The only way my code compiles is if I don't do any of this, as I couldn't figure out how to do it – Ahed N Mar 21 '20 at 20:20
  • Just to rephrase what I'm trying to achieve: User inputs: John. Code creates the vector john inside of a main vector (could be named allStudents) that holds all the different vectors of students (which in turn hold int values for test scores) – Ahed N Mar 21 '20 at 20:23
  • I appreciate you going back and forth with someone who is new the language, that means a lot to me! – Ahed N Mar 21 '20 at 20:24
  • If you want to use `getline` to read from `cin` and put it in a `string` called `name`, you do `std::getline(std::cin, name)`. Look at the documentation for [`getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) – JohnFilleau Mar 21 '20 at 20:47
  • So what I'm hearing is for each student, you want a `std::vector` to hold the grades. And you'd like to be able to retrieve the grades based on each student by name. That sounds like you want a `std::map>`. – JohnFilleau Mar 21 '20 at 20:50
  • Thanks once again for not giving up on me haha, I'm not home right now, but I'll give `std::map` a try, and see if it works for me/I can figure it out. Appreciate all the help! – Ahed N Mar 21 '20 at 21:03

0 Answers0