Reading contents of a txt file, sorting it into an array

I'm trying to load the contents of a text file into an array of employees.

Contents of text file are as follows:
10 alice 4/23/1972 123-45-6789 support assistant
3 bob 6/7/1980 111-12-1134 logistics manager
1 carol 10/2/1963 987-123-1143 admin ceo
2 dave 10/3/1974 902-22-8914 admin cfo
17 erin 6/13/1991 126-83-1942 technology supervisor
15 frank 2/22/1987 303-12-1122 logistics assistant


The data represents ID#, name, DOB, SSN, department, and position.
Not really sure why I'm getting an error. Shouldn't the while loop simply cycle through all strings and assign them to employee New variables?

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>
using namespace std;

struct employee
{
    int id;
    string name;
    string dob;
    string ssn;
    string dept;
    string pos;
};

class log
{
private:
    int id;
    string date;
    int timeIn;
    int timeOut;
};

int main()
{
    string str;
    employee New;
    employee array[100];
    int i = 0, employeenum = 0;
    
    fstream employeeList;
    employeeList.open("details.txt");
    
    //Each time a new variable is read, the while loop will
    //determine what type of variable it is.
    while (employeeList >> str)
    {
        if (i == 0)
        {
            New.id = str;
            i++;
            continue;
        }
        if (i == 1)
        {
            New.name = str;
            i++;
        continue;
        }
        if (i == 2)
        {
            New.dob = str;
            i++;
            continue;
        }
        if (i == 3)
        {
            New.ssn = str;
            i++;
            continue;
        }
        if (i == 4)
        {
            New.dept = str;
            i++;
            continue;
        }
        if (i == 5)
        {
            New.pos = str;
            array[employeenum] = New;
            employeenum++;
            i = 0;
            continue;
        }
    }

    
    //I'm testing the code with this line, but it's outputting
    //an error whenever I run the program.
    cout << array[0].pos << endl;
}



Thanks,
Jon
Last edited on
Such is the problem with using namespace std;. There is a std::string in iostream, just not one that allows for the >> operator. This makes it hard to notice you forgot #include <string>

Line 39 is simply a problem, you try to assign a string to an integer.
Thanks so much. I changed the ID to a string instead of int.

I'm still running into problems when opening the .txt file, even though the .txt is in the same directory as the project.
Another problem:
I have another .txt with the following content:

10 2/11 0900 1700
3 2/11 0930 1730
1 2/11 1100 2000
2 2/11 1000 1530
17 2/11 0900 1700
10 2/12 1000 1830
3 2/12 0930 1730
1 2/12 1100 1900
2 2/12 1030 2000
17 2/12 0900 1700
15 2/12 1100 1600

The first int represents employee ID, the next is the date, next is time in, next is time clocked out.

I'm trying to write a loop to cycle through the .txt and store the values in an array of class log

1
2
3
4
5
6
7
8
class log
{
private:
    string id;
    string date;
    int timeIn;
    int timeOut;
};


How can I do this? For the first part of the program, it was easy because all of the variables were strings, but I'm not sure how to assign the contents of the .txt to their appropriate variables.

Thanks!!
Jon
The >> operator returns the istream (cin, ifstream) that used it. It is therefor possible to chain it:
1
2
3
4
int x, y;
std::cout << "Type a number and then another number\n: ";
std::cin >> x >> y;
std::cout << "You typed: " << x << " and " << y << '\n';


Or perhaps in your case:
1
2
3
4
5
6
std::string id, date;
int timeIn, timeOut;

std::ifstream data("myData.txt");
while (data >> id >> date >> timeIn >> timeOut)
  // do things 


Which might make you reconsider the way you have the code you posted.

I assume you haven't learned about operator overloading, but you can actually overload the >> operator:
http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
Last edited on
Topic archived. No new replies allowed.