Filling a several vectors from a file with ints and strings

I am currently working on another lab and I can't seem to figure out how to fill these vectors with the following input from a text file:

1000 George Washington 10000
2000 John Adams 15000
1212 Thomas Jefferson 34000
1313 Abraham Lincoln 45000
1515 Jimmy Carter 78000
1717 George Bush 80000

The first number in each row is the "employee id number" and the other number is their salary. I realize that the reason the vectors are not filling properly is that the input can't handle the strings going into the int slot (input_file >> input_numbers) and the strings can't handle numbers (input_file >> input_strings), but I am uncertain how to go about solving that.

Later I will have to figure out how to print out the contents as well, but that doesn't seem like it will be much a challenge

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
83
84
85
86
87
88
89
90
91
92
 #include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

void load_data(vector <int>& ID, vector <string>& Name, vector <double>& salary);
void print_data(vector <int> ID, vector <string> Name, vector <double> salary);

int main()
{   
    string file_name;
    vector <int> ID;
    vector <string> name;
    vector <double> salary;
    load_data(ID, name, salary);
    print_data(ID, name, salary);
    return EXIT_SUCCESS;




}

void load_data(vector <int>& ID, vector <string>& Name, vector <double>& salary)
{
    ifstream inputfile;
    int input_numbers = 0;
    string input_strings;
    string file_name;
    cout << "\nPlease enter the name of the file containing your employeer data: ";
    cin >> file_name;

    inputfile.open(file_name);
    if (inputfile.fail())
        {
            cout << "\nFile failed to open. Please re-enter the file name: ";
            load_data(ID, Name, salary);
        }
    
    while(inputfile >> input_numbers)
    {
        if(input_numbers < 10000)
        {
            ID.push_back(input_numbers);
          
        }
    }

    inputfile.clear();
    inputfile.seekg(0, ios::beg);


    while(inputfile >> input_strings)
    {
        Name.push_back(input_strings);
      
    }

    inputfile.clear();
    inputfile.seekg(0, ios::beg);


   while(inputfile >> input_numbers)
    {
        if(input_numbers >= 10000)
        {
            salary.push_back(input_numbers);
            
        }
    }
    inputfile.clear();
    inputfile.seekg(0, ios::beg);
    inputfile.close();

}

void print_data(vector <int> ID, vector <string> Name, vector <double> salary)
{   
    
    cout << "ID      Name                Salary \n";
    cout <<"____________________________________\n"; // this function isn't 
    for( int loop : ID)                              // finished yet
    {

        cout << loop << " ";
    }

}


You already know the format of what you're taking in. Instead of three loops, you should have just one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while(!inputfile.eof())
{
    int temp;
    inputfile >> temp;
    ID.push_back(temp);

    std::string first, last;
    inputfile >> first >> last;
    first += " ";
    first += last;
    Name.push_back(first);

    int salTemp;
    inputfile >> salTemp;
    salary.push_back(salTemp);
}


^Something like that is probably what you're looking for.
Last edited on
So are you relying on the actual position of how these things come up then?
Yes, since it comes in predictably. They likely expect you to see this pattern in the text file and use it to your advantage. Making a program to take in input that's un-ordered is more difficult, and very likely not what they want you to do.
Topic archived. No new replies allowed.