Text File with strings and integers to an 2d Array

Hello, I want to read a txt file that contains intgers and string in c++ prgram.
i know how to do it with one single array with intgers or strings only.
can you give an idea how to combine those two in one file and one array.

text file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1	Arthur Adams	s	1000.00	00	00.00	00000.00	20000.00
2	Bill Boston	h	000.00	37	10.00	00000.00	61000.00
3	Chad Collins	c	000.00	00	00.00	30000.00	49500.00
4	Diane Devens	h	000.00	45	15.00	00000.00	49800.00
5	Emily Easter	s	1500.00	00	00.00	00000.00	21500.00
6	Fran Fox	c	000.00	00	00.00	45000.00	33000.00
7	Gordon Gold	h	000.00	56	12.00	00000.00	72000.00
8	Harry Haven	h	000.00	88	20.00	00000.00	50000.01
9	Ira Irish	s	763.54	00	00.00	00000.00	49450.00
10	Jan Jones	c	000.0	00	00.00	72000.00	48485.00



It depends on how the text file is formatted. At first glance this looks like a fixed-width layout. You could read each line into a structure which you define, or read each line a portion at a time, specifying the length to read. That can get a bit messy, but a lot of the code is repetitive so once you start, it gets easier.

On the other hand, it looks as though this text is separated into individual fields using the tab character '\t'. In this case, things are probably easier. You can use the getline() function with '\t' as the delimiter, and read each field into a string, and use a suitable function such as atoi() or atof() to convert to numeric. Or something along those lines, more than one possibility.

As for storing the result in an array, I'd suggest you define a suitable structure, such as this:
1
2
3
4
5
6
7
8
9
10
struct item {
    int  num1;
    string name;
    char letter;
    double num2;
    int num3;
    double num4;
    double num5;
    double num6;
};


Then you just need an array of such items to hold the resulting data.
Topic archived. No new replies allowed.