Why is it not printing the names

I am trying to put information from a file into arrays but I am not getting it right. The data is not getting saved into any of the arrays. What am I missing to save the data correctly.

Info in file:
Schweller Poobah 72 1 79
Wylie Czar 65 68 81
Kim Boss 77 70 85
Ayati Capitan 95 92 97
Zhang Honcho 93 98 91
Yu Chief 90 96 93
Egle Vizier 87 82 89
Dietrich Bigwig 85 86 88

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

using namespace std;
void saveData(string a[], string b[], int c[], int d[], int e[]){
  ifstream inFile;
  for(int i = 0; i <= 8; i++){
    inFile >> a[i] >> b[i] >> c[i] >> d[i] >> e[i];
  }
}
int main() {
string names[8];
string titles[8];
int review1[8];
int review2[8];
int review3[8];

ifstream inFile;
inFile.open("employees.txt");
if(!inFile){
  cout <<"Error" << endl;
  exit(1);
}

saveData(names, titles, review1, review2, review3);
cout << names[0];

return 0;
}
There are a lot of things wrong here.

In terms of your question you open an input stream in main(), but saveData() (odd name for something that reads data) knows nothing about this stream; indeed it tries, fruitlessly, to declare another, which isn't connected to a file. You could pass the stream from main() to saveData() as a reference argument, or you could open the stream within saveData() - but don't do both.

Beyond that, your arrays have 8 elements, indexed from 0 to 7, so line 9 is a recipe for disaster.

I think I would be looking to use a single array of structs rather than parallel arrays, but that's for another day. Likewise, the number of employees is unlikely to be fixed, so a std::vector would be better than a fixed-size array, especially with the "magic number" 8 appearing here there and everywhere.
Last edited on
Just as @lastchance wrote - you have demonstrated a lot of problems.
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const int LIMIT{20};

int saveData(std::ifstream& inf, string a[], string b[], int c[], int d[], int e[])
{
    int count = 0;
    while(inf >> a[count]>> b[count] >> c[count] >> d[count] >> e[count])
    {
        std::cout << a[count] << ' ';
        count++;
    }
    std::cout << '\n';
    
    return count;
}


int main()
{
    string names[LIMIT];
    string titles[LIMIT];
    int review1[LIMIT];
    int review2[LIMIT];
    int review3[LIMIT];
    
    ifstream inFile;
    inFile.open("employees_1.txt");
    if(!inFile)
    {
        cout <<"Error" << endl;
        exit(1);
    }
    
    int count = saveData(inFile, names, titles, review1, review2, review3);
    
    cout << names[0]  << '\n';
    
    return 0;
}



Schweller Wylie Kim Ayati Zhang Yu Egle Dietrich 
Schweller
Program ended with exit code: 0
Topic archived. No new replies allowed.