Help parsing string into array of structs

Hello guys, I need help with a little problem I got.

I want to parse multiple strings into an array of structs.

The string I got looks like this

 
"Olivia Montgomery*female*47;John Newton*male*34;Carla Walton*female*43;"


My struct looks like this

1
2
3
4
5
6
struct person_info
{
    std::string name;
    std::string gender;
    std::string age;
} pinfo[MAX_PERSON_INFO];


so what I want to do is make an array of those structs, so I can access it like this for instance

 
pinfo[1].name


and I should get 'John Newton' then.

I've tried to do this already but didn't suceed, I am writing this from a browser and dont have access to my IDE to show my previous attempts.

Would really like to see your solutions! Thanks in advance :)
Here is my quick and dirty solution for the first third of the string and a single person_info object. You'll obviously have to apply it to the full string (most likely using a loop), and use indices as you will have an array of person_info objects.

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
#include <iostream>
#include <string>

struct person_info
{
    std::string name;
    std::string gender;
    std::string age;
} pinfo;

int main()
{
    std::string str = "Olivia Montgomery*female*47;John Newton*male*34;Carla Walton*female*43;";

    // name is from position 0, to until first * found
    pinfo.name = str.substr(0, str.find("*", 0));
    str = str.substr(str.find("*", 0)+1, str.length()); // new string

    // gender is from position 0 to until first * found
    pinfo.gender = str.substr(0, str.find("*", 0));
    str = str.substr(str.find("*", 0)+1, str.length()); // new string

   // age is from position 0, to until ";" found
    pinfo.age = str.substr(0, str.find(";", 0));
    str = str.substr(str.find(";", 0)+1, str.length()); // new str

    // prints name, gender, age
    std::cout << pinfo.name << ", " << pinfo.gender << ", " << pinfo.age << "\n";
}
Last edited on
Thank you very much for your reply, this matches what I have done before, but your approch seems a little smarter, ill try it out in a loop later today
Here is another quick and dirty way to do it:
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

const int MAX_PERSON_INFO = 3;

struct person_info
{
  std::string name;
  std::string gender;
  std::string age;
} pinfo[MAX_PERSON_INFO];

int main () 
{
  string s ("Olivia Montgomery*female*47;John Newton*male*34;Carla Walton*female*43;");
  istringstream iss (s);
  string buffer;
  int i = 0;
  while (getline (iss, buffer, ';') && i < MAX_PERSON_INFO)
  {
    istringstream ls (buffer);
    getline (ls, pinfo[i].name, '*');
    getline (ls, pinfo[i].gender, '*');
    ls >> pinfo[i].age;
    i++;
  }

  for (auto p : pinfo)
  {
    cout << "\nName = " << p.name;
    cout << "\nGender = " << p.gender;
    cout << "\nAge = " << p.age << '\n';
  }

  system ("pause");
  return 0;
}
KISS(Keep it simple,stupid)
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
#include <array>
#include <vector>
#include <string>

struct Person
{
	std::string name;
	std::string gender;
	std::string age;
	Person(std::array<std::string, 3>& _array)
		: name(_array[0])
		, gender(_array[1])
		, age(_array[2])
	{
	}
};

struct PersonManager
{
	std::vector<Person> persons;
	PersonManager()
		: persons()
	{
	}
	void SendInfo(std::array<std::string, 3> _singleInfo)
	{
		persons.push_back({ _singleInfo });
	}
};

int main(int argc,char* argv[])
{
	PersonManager manager;
	manager.SendInfo({ "Olivia Montgomery", "female", "47" });
	manager.SendInfo({ "John Newton", "male", "34" });
	manager.SendInfo({ "Carla Walton", "female", "43" });
        return 0;
}
Topic archived. No new replies allowed.