I'm a beginner, so I'm sorry if this is really dumb question/problem. The assignment that I have is printing out a dynamic array from an input file. I tried googling it and I found some similar problems... but the answers were all like "use vectors" etc but we haven't learned those yet. It's also said that a function must be used. This is what I came up with:
#include <iostream>
#include <fstream> //file input
using namespace std;
int *out(int *arr, int siz){
arr = new int[siz];
for (int i = 0; i < siz; i++) {
cout << arr [i] << " ";
}
return arr; //this should print out the array later???
}
int main(){
int siz;
int *arr;
ifstream inf ("input.txt");
inf >> siz; //
for (int i = 0; i < siz; i++) {
inf >> arr[i];
}
inf.close();
cout << "This array contains following elements: ";
*arr = *out(arr, siz) ;
delete[] arr;
return 0;}
So, it doesn't give any errors with Dev-C++ but when I try to run it, it crashes. I tried debugging it and then it gave me "segmentation error" or something like that. Then of course, I googled it and there must be something wrong with the pointers, right? Could you help me out? Thanks.
g++ -Wall -g). Then use std::vector. Learn how to use the debugger (gdb)