void function error!

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int ARRAY_SIZE = 1000;

struct Data{
string bookTitle[ARRAY_SIZE];
string bookAuthor[ARRAY_SIZE];
};

void loadData(Data lines[28]);
void showAll (Data lines[28]);
//int showBooksByAuthor (int count, string name);
//int showBooksByTitle (int count, string title);

int main(){
const int ARRAY_SIZE = 1000;
Data lines[28];
char choice;

loadData(lines);

do{
cout << "\nEnter (Q)uit, Search (A)uthor, Search (T)ittle, (S)how All: " ;
cin.get(choice);
cin.ignore();
switch(choice){
case 'Q':
cout << "Terminating program ... Thank you for choosing Giac's Library Database. " << endl;
break;
/*case 'A':
showBooksByAuthor;
break;
case 'T':
showBooksByTitle;
break; */
case 'S':
showAll(lines);
break;
default:
cout <<"Not a valid choice. " << endl;
}
} while ( choice != 'Q');

getchar();
getchar();
return 0;
}

void loadData(Data lines[28]){
fstream inFile;
string pathName;
int count = 0;
cout << "Welcome to Giac's Library Database. \n";
cout << "Please enter the name of the backup file: ";
getline(cin, pathName);
inFile.open(pathName);
if(inFile.is_open()){
for (int i = 0; i < 28; i++){
getline(inFile, lines[i].bookTitle[ARRAY_SIZE]);
getline(inFile, lines[i++].bookAuthor[ARRAY_SIZE]);
count++;
}
cout << count << " reccords loaded successfully. " << endl;
}
else {
cout << "Invalid file path." << endl;
}
}

void showAll (Data lines[28]){
for (int i = 0; i < 28; i++){
cout << lines[i].bookTitle[ARRAY_SIZE]<< endl;
cout << lines[i++].bookAuthor[ARRAY_SIZE]<< endl;
}
}


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:
test dword ptr [eax],eax ; probe page.
When I set it back to lines[14], it is still good to compile. Please let me know is there anything wrong with my code?
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
 
    lines[i].bookTitle[0]
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.

Last edited on
Thank you for your reply to explain for me where my mistake is.
Topic archived. No new replies allowed.