Storing values in arrays

I'm am trying to get info from a file and put it into three arrays but it isn't working and I have no idea what is wrong.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;



void sort(ifstream& infile){
int i=0;
string firstname,lastname,birthdate;
while(infile>>firstname>>lastname>>birthdate){

i++;

}

string firstname1[i],lastname2[i],birthdate3[i];
for (int j=0; j<i;j++)
{
    infile>>firstname1[j];
    infile>>lastname2[j];
    infile>>birthdate3[j];
    cout<<firstname1[j]<<lastname2[j]<<birthdate3[j]<<endl;
}

}
int main (){
string infile_name;
ifstream infile;

cout << "What file?";
cin>>infile_name;
infile.open(infile_name.c_str());

sort(infile);


}
On lines 12-16, you read the entire file to the end. On line 18, you have three syntax errors. On lines 19-25, you use the same file stream that is already at the end of the file.

Use a std::vector.
I need to use arrays. What are the syntax errors?
Size of stack allocated arrays should be known at compile time, i.e. you can do this:
1
2
const int i = 10;
int array[i];
but not that:
1
2
3
int i;
std::cin >> i;
int array[i];


Name sort is misleading: it does not actually sorts anything.
Using parallel arrays is prone to errors, better to group your fields in struct and use single array.
Last edited on
Topic archived. No new replies allowed.