jeudi 6 février 2020

How can I read specific lines of a text file into an array of type int in C++?

I need to read text files into an array. The text files are formatted so that the first line contains the array size and the following lines are the elements of the array:

    6
    5
    3
    2
    1
    6
    4

Where the array size equals 6. The files need to be read in this format:

a.exe < testfiles/input

Here's what I have so far:

int main(){

    int arraySize = 0;

    cin >> arraySize;
    int array[arraySize];

    for(i = 1; i < arraySize; i++){
        cin >> array[i];
    }

    for(j = 0; j < arraySize-1; j++){
        //insertion sort here
    }

}

The array remains all zeros but is the correct size. Can anyone see my error?

Aucun commentaire:

Enregistrer un commentaire