I am trying to read in names from a file that is given to me. The number of names that are read is based on user input. This compiles fine, but when I run it I get this error:
"0xC0000005: Access violation writing location 0xabababab."
I have done some research and I think it's probably because I am writing over the bounds of the array, but I cannot figure out why this is the case.
Before anyone asks I was told that I have to use string* for my array. I am not to use vectors or array of characters. Thank you.
int Capacity;
int numNames = 0; // wasn't in original question, but was in the code
cin >> Capacity;
string name;
string* arr = new string[Capacity];
while( !fs.eof() && numRead < Capacity)
{
getline( fs , names);
arr[numRead] = names; // error thrown here
arr++;
numRead++;
}
arr++Could also just change your loop to do:std::getline(fs, arr[numRead++]);and get rid ofnamesand everything else.. Don't forget to calldelete[] arrwhen finished with it.!fs.eof(), it's the wrong check. instead, terminate the loop whengetlinefails.