0

I'm trying to use an object name from the array. I know I wrote wrong code below inside the loop. How should I write code on the below pointer lines for using object name from the array?

enter image description here

Here's the full code:

#include <iostream>
using namespace std;

class Students{
    public:
        int index;
        string name;
        string father;
};
int main() {
    string nname[] = {"John", "Doe"};
    string fname[] = {"Ex John Father", "Ex Doe Father"};

    int NarrSize = sizeof(nname)/sizeof(nname[0]); // Size of nname array
    int FarrSize = sizeof(fname)/sizeof(fname[0]); // Size of fname array

    if( NarrSize == FarrSize ){ // Check if both array size is same
       for(int i=0; i<NarrSize; i++){
            Students nname[i];

            nname[i].index  = i+1;
            nname[i].name   = nname[i];
            nname[i].father = fname[i];
        }
    }

  return 0;
}
3
  • 4
    Students nname[i]; is not legal in c++ among other things. Like an off by 1 error and it goes out of scope at the next iteration Commented Nov 6, 2020 at 19:34
  • A variable name in c++ must be known at compile time its not something that exists or can change at runtime Commented Nov 6, 2020 at 20:11
  • Perhaps what you really want is std::map<std::string,Students> Commented Nov 6, 2020 at 20:36

1 Answer 1

4

You should allocate the array before the loop.

Also the name nname is already used as the array of string, so you should give another name to your array of Students.

Using non-standard VLA (variable-length array):

    if( NarrSize == FarrSize ){ // Check if both array size is same
       Students student_array[NarrSize]; // allocate array
       for(int i=0; i<NarrSize; i++){

            student_array[i].index  = i+1;
            student_array[i].name   = nname[i];
            student_array[i].father = fname[i];
        }
    }

Using std::vector instead:

    if( NarrSize == FarrSize ){ // Check if both array size is same
       std::vector<Students> student_array(NarrSize); // allocate vector
       for(int i=0; i<NarrSize; i++){

            student_array[i].index  = i+1;
            student_array[i].name   = nname[i];
            student_array[i].father = fname[i];
        }
    }

Add #include <vector> to use std::vector.

Sign up to request clarification or add additional context in comments.

4 Comments

Well, in both cases, we now have a collision between the Students collection named nname and the std::string collection also named nname. But yes, it seems like the ultimate goal is to try to create a collection of Students and this will do that with a different variable name.
@NathanPierson Thank you for notifying. Collision fixed.
@MikeCAT Inside the for-loop, every increment should get a different object name as array value. If I remove the first line and don't specify the name inside of the for-loop so where the value will assign? because nname has 2 array values and each value should be an object for Students class.
@Naiem I don't understand your concern. My advice is to try the second of the 2 example codes here however to make it valuable you actually need to do something with the vector after the loop fills it. Perhaps a range based for loop that prints all items in the vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.