I'm trying to pass a dynamic array into a function and fill it but i can't figure out how to do it correctly and i can't find any good literature on it either... here is my code.
//////////////////////////////////////////////////////////////////////
// An Excercise in Pointers: Blood Donations! //
//takes previously generated data file and displays information //
//in an easy to see and analyze format //
//Programmer: Tyler Sievers //
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace std;
void getdata(int list, int& count);
void main()
{
int *Donation_List;
int *Size;
Size = newint;
*Size = 0;
Donation_List = newint[*Size];
//at this point i have declared my dynamic array
//as per instructions Size will be a pointer and
//is currently set to zero as there are no elements
//currently in the array size will also end up
//being the total number of donors when the array
//is finished being populated
getdata(*Donation_List, *Size);//opens file gets data from file and closes file
//at this point size is the total number of donors!
cout<< *Size;
}
void getdata(int list, int &count)
{
ifstream data;
data.open("donations.txt");
if(data.fail())
{
cout<< "Failed to open 'donations.txt' please make sure that the file is accessable and try again.";
exit(1);
}
while(!data.fail())
{
data >> list[&count];
count++;
}
data.close();
}
When i run it the console just crashes... When i debug the crash comes at the very first data>>list[&count];... I'm wondering if &count is putting the address in the index there and its crashing because it isn't a valid one... I don't know how to fix this though...
I'm wondering if &count is putting the address in the index there and its crashing because it isn't a valid one...
Correct. This is the same as (&count)[list] and obviously doesn't do what you want.
I don't think you understood what dynamic allocations are - Arrays cannot be resized after they've been created, whether they were allocated dynamically or not. Dynamic "arrays" are called vectors in C++ and you can use push_back to add elements to them. See here: http://www.cplusplus.com/reference/stl/vector/
main() must return int, by the way. void is not allowed.
yeah i know that main is supposed to return int my teacher doesn't though -.-
So vectors are off limits for me and my arrays are to use exactly the right amount of space for the data in the file without actually knowing how much data is in the file. I don't understand how i am to accomplish this? I am sure he wants an array based structure also so what can i do to make a data structure that will scale as data is added from zero to whatever is necessary without making a preliminary guess... I'm not asking for code just some ideas to run with...
I feel like that is terribly inefficient but limited from vectors as i am i guess that is what i will have to do... i could write a data structure class but that would take time and i would have to violate rules of encapsulation to do some of the stuff that he is requiring i do... maybe i will look into that a little bit tomorrow... thanks for the help Athar!