How to read int from file into size of the array?

Sep 27, 2014 at 10:40pm
I'm trying to read an integer from a file, and use it in an array.
here's part of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string *firstName;
string *middleName;
string *lastName;
int *ageElected;
string *stateOfBirth;
string fileName = "Lab4a_data.txt";
int n;
fstream file;

openIOFile(file, fileName, ios::out|ios::app); //file opens

file >> n;

firstName = new string[n];  //debug error occurs here
middleName = new string[n];
lastName = new string[n];
ageElected = new int[n];
stateOfBirth = new string[n];


the start of the file "Lab4a_data.txt" is:
41
George::Washington:
57
Virginia

41 is the number of records.
The number 41 is what I'm trying to assign to n.
Sep 28, 2014 at 12:26am
You say an error occurs, but you haven't said what the error was. What is the error?
Sep 28, 2014 at 3:57am
A Microsoft Visual C++ Debug Library window pops up:

Debug Error!
Program:...
Invalid allocation size: 4294967295 bytes
(Press Retry to debug the application)

I click retry then...

President_Wulff.exe has triggered a breakpoint

I click continue...

Unhandled exception at 0x74c6c42d in President_Wulff.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003ffbd4..

then the Debug Error! again.

Maybe some kind of run-time error.
Sep 30, 2014 at 12:31pm
I got some help, and found out that somehow I deleted the text file. I added a new text file and now everything is working fine.
Topic archived. No new replies allowed.