0

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?

EDIT: I've fixed the array to be dynamically allocated. The program now executes without an error, but now the entire array is full of the second entry of the text file, in this case, 5.

  • 3
    The size of an array has to be a constant expression a.k.a known at compile time. Use `vector` instead if you only know the size at runtime. – Moshe Rabaev Feb 06 '20 at 19:30
  • Use a `std::vector` since you don't know the size till run time. – Shawn Feb 06 '20 at 19:31
  • The piece of knowledge you are missing to solve this issue is stack vs. heap memory allocation. You cannot do dynamic-sized arrays on the stack. When you declare an array like you've done, that's stack-based allocation. You can only declare dynamic sized arrays on the heap using the `new` keyword. See: https://stackoverflow.com/a/1598409/2516916 – Gillespie Feb 06 '20 at 19:33
  • 2
    This is invalid C++ code. Array size must be an integral const expression. Use a dynamic array or use std::vector. – ThomasMcLeod Feb 06 '20 at 19:34
  • `int array[arraySize];` is not valid standard C++. VLAs are a compiler extension, *not* a language feature. Use a `std::vector`. – Jesper Juhl Feb 06 '20 at 19:48
  • How is this code even compiling? You have not declared a type for `i` – Gillespie Feb 06 '20 at 19:49
  • I declared several variables like i, j, k, and key, but edited them out for this post, sorry – Noah McCullah Feb 07 '20 at 00:45
  • When you create an array of `array[arraySize]` the indicies run from `0` to `arraySize-1` so a loop of `for(i = 1; i < arraySize; i++)` loops one less value than the number of things in the file. It should be `for(i = 0; i < arraySize; i++)` – Jerry Jeremiah Feb 07 '20 at 02:18

2 Answers2

2

Since this question reads like a homework assignment, I'm guessing you are not allowed to use std. On the off chance you are, however, here is a much safer way to accomplish what you are attempting:

#include <iostream>
#include <vector>

using namespace std;

int main(){
    vector<int> nums;

    int arraySize;
    cin >> arraySize;

    for(int i = 0; i < arraySize; i++){
        int value;
        cin >> value;
        nums.push_back(value);
    }

    for (auto num : nums)
        cout << num << " ";

    return 0;
}
Gillespie
  • 5,780
  • 3
  • 32
  • 54
0

In a C++ array declaration, array size must be an integral const expression. Simplest change is to use a dynamic array:.

int main(){
    using namespace std;
    int arraySize = 0;

    cin >> arraySize;
    int * array = new int[arraySize];

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

    for(int j = 0; j < arraySize-1; j++){
        //insertion sort here
    }
    delete [] array;
}
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80