0

File Reading

How i can read an array which has n elements. n is read from a text file a's elements is also read from the same text file. I need to read them through a function. Here is my code, Code Block cant compile it, can someone explain for me. Thank you. #include <iostream> #include <fstream> using namespace std; void input(int &n,int a[]) { ifstream fi("MAX.INP"); fi >> n; for(int i=0;i<n;i++) { fi >> a[i]; } fi.close(); } int main() { int n; int a[n]; input(n,a); return 0; } There are no warning messages from Code Block

15th Oct 2017, 10:12 AM
thong nguyen
thong nguyen - avatar
3 ответов
+ 3
int a[n] is not valid because the compiler cannot determine n at compile time. You need to use a dynamic array instead, or use STL containers such as unique_ptr or vector.
15th Oct 2017, 10:30 AM
aklex
aklex - avatar
0
Then how i can read n and a'element without vector or dynamic array ?
15th Oct 2017, 2:53 PM
thong nguyen
thong nguyen - avatar