Several thoughts arise here. The first is trying to understand the design.
1 2 3 4 5 6
|
const int ARRAY_SIZE = 1000;
struct Data{
string bookTitle[ARRAY_SIZE];
string bookAuthor[ARRAY_SIZE];
};
|
Here a single
Data
object can contain 1000 separate book titles, and 1000 separate book authors.
My first thought on seeing that, is that if (as is presumably the case) there is a one-to-one correspondence between title and author, then why not do this instead
1 2 3 4 5 6 7 8
|
const int ARRAY_SIZE = 1000;
struct Book{
string Title;
string Author;
};
Book Data[ARRAY_SIZE];
|
Here, each book has a single title and a single author, and the array named Data contains 1000 of them.
Now on to the problems with your code.
There is a serious issue here:
1 2 3 4 5
|
for (int i = 0; i < 28; i++){
getline(inFile, lines[i].bookTitle[ARRAY_SIZE]);
getline(inFile, lines[i++].bookAuthor[ARRAY_SIZE]);
count++;
}
|
Array subscripts start from 0. The first bookTitle is
and the last is
|
lines[i].bookTitle[ARRAY_SIZE - 1]
|
Note the - 1 in there.
Your code is thus attempting to modify the 1001st array element, this is an out of bounds error, attempting to access memory outside the array.
You could probably fix your existing code by simply always using
lines[i].bookTitle[0]
but then the question would arise - what is the purpose of that array of 1000 strings?
Oh, I almost forgot:
When I set Data lines[14] for all my code, it compiled, but it just read 14 lines of my file. Therefore I set it to 28, and I got an error message: |
It looks like your arrays of 28*(1000+1000) = 56000 strings are too large to store on the stack. If you really really needed to allocate 56000 strings, then you could use dynamic allocation (std::vector recommended) but I'd fix the design problems first.