Read from TXT file to data structure

I trying to create a program that takes a file that consists of an amount of Employees, then each employee ID, First Name, Last Name, Amount of Sales, and then each sale. Here is an example:

42

1 John Curry 5 2345.34 345.40 569.89 4560.45 450.00
2 Abigail Right 0
3 Jack Monstrom 2 34569.00 3456.45
4 Mary Apple 3 57050.23 2565.45 3980.56
5 Joe Smalls 3 55035.24 3000.58 3756.72

etc...

42 Mike Paperino 4 6002.67 2035.89 1895.23 785.20

with this I would like to create a structure and put each item into that structure when the file is read.

Right now I am having an issue because some of my structure attributes are integers and my IDE is saying that won't work.

Here is what I have so far:


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>

using namespace std;

struct employee_rev{
    string First_name;
    string Last_name;
    int UIN;
    int Num_sales;
    float Sales;

}employee;



int main() {

    string full_line;
    string substringTWO;
    string substringTHREE;
    string substringFOUR;
    string substringFIVE;
    string substringSIX;

    ifstream origfile ("Info.txt");
    if (origfile.is_open())
    {
        while( getline (origfile,full_line, '\n'))
        {
            size_t L = full_line.length();
            size_t p = full_line.find(" ");

            employee.UIN = full_line.substr(0,p-1);
            substringTWO = full_line.substr(p+1, L-1);

            employee.First_name = substringTWO.substr(0, p-1);
            substringTHREE = substringTWO.substr(p+1, L-1);

            employee.Last_name = substringTHREE.substr(0, p-1);
            substringFOUR = substringTHREE.substr(p+1, L-1);

            employee.Num_sales = substringFOUR.substr(0, p-1);
            substringFIVE = substringFOUR.substr(p+1, L-1);





        }

    }

    else cout << "Unable to open file";

    return 0;
}
Before you get to assigning anything to your struct, start with
1
2
3
4
5
6
7
8
9
10
11
            cout << "\nemployee.UIN =" << full_line.substr(0,p-1);
            substringTWO = full_line.substr(p+1, L-1);

            cout << "\nemployee.First_name =" << substringTWO.substr(0, p-1);
            substringTHREE = substringTWO.substr(p+1, L-1);

            cout << "\nemployee.Last_name =" << substringTHREE.substr(0, p-1);
            substringFOUR = substringTHREE.substr(p+1, L-1);

            cout << "\nemployee.Num_sales =" << substringFOUR.substr(0, p-1);
            substringFIVE = substringFOUR.substr(p+1, L-1);


When you're confident that your string splitting is delivering correct results all the time, then you can start assigning to the structure.

For those int members, use this on the string.
http://www.cplusplus.com/reference/string/stoi/
Topic archived. No new replies allowed.