I'm a beginner and maybe the solution here is easier that I think it is, but I have spend a lot of time trying to fix this problem. The main goal is to create a dynamic array from a file.
I have this code:
#include <iostream>
#include <fstream>
using namespace std;
void out(int *arr, int siz){
for (int i = 0; i < siz; i++) {
cout << arr [i] << " ";
if((i + 1) % 5 == 0){
cout << endl;
}
}
}
int main(){
int *arr;
int i = 1;
int len = 0;
ifstream fin;
fin.open("input.txt");
int s;
fin >> s;
arr = new int[s];
while(!fin.eof()){
fin >> arr[i];
len++;
i++;
}
fin.close();
out(arr, len);
delete[] arr;
return 0;
}
And file looks like this:
11
84
65
80
62
98
2
4
75
69
But the output looks like this:
0 84 65 80 62
98 2 4 75 69
The problem is that it counts first element as 0, instead of 11.
Expected output:
11 84 65 80 62
98 2 4 75 69
I have tried changing parts of the code, but I still didn't manage to find the proper solution. Do you have any ideas what I'm doing wrong?