简体   繁体   中英

How do I programatically create multiple structs in C++

I am trying to read in data from a.txt file and use it to create several instances of a struct. I want to do this progromatically via code, as opposed to declaring each instance of the struct manually in a pre determined way (I want it to work for any number of structs being created/data rows in the input file).

The input file contains a name and then three doubles. Each new line is a new person ("input.txt"):

Peter 1.0 2.0 3.0
James 4.0 5.0 6.0

Code:

struct Person 
{
  string name;
  vector<double> numbers;
};


int main()
{

ifstream inStream("input.txt");

vector<Person> vecPerson; // vector to hold structs
string nameInput;
double num1;
double num2;
double num3;

int i =0;
while( inStream.peek() != EOF )
{

    inStream >> nameInput >> num1 >> num2 >> num3; //read in data to variables

    //create struct
    Person person[i];
    person[i].name = nameInput;
    person[i].numbers[0] = num1;
    person[i].numbers[1] = num2;
    person[i].numbers[2] = num3;
    i++;

    vecPerson.push_back(person[i]);
}  

This code gives a segfault. The line Person person[i]; clearly isn't right syntactically. I am not sure how I create a different name for the struct instance for each loop of the while loop.

How do I do this?

There is no reason to name the Person struct in the loop at all. You can simply push_back a Person onto the vector:

while( inStream.peek() != EOF )
{
    inStream >> nameInput >> num1 >> num2 >> num3; //read in data to variables

    vecPerson.push_back(Person{nameInput, {num1,num2,num3}});
}  

This avoids naming the structs inside the loop, or using the index i at all.

Also, the condition of your loop is incorrect. You should simply do:

while(inStream >> nameInput >> num1 >> num2 >> num3) //read in data to variables
    vecPerson.push_back(Person{nameInput, {num1,num2,num3}});

Now each Person in the vector has a "different name" by virtue of being able to index a Person at a particular position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM