I don't know why this crashes my program if I have more than 10 items read into the array of objects from a file? I did this exact code in another program just fine and it would stop reading as soon as the limit of the intialized element number was reached for the array of objects. The only difference this time around with this code is that I am now using a friendship to access the private variables and read data into the private variables DIRECTLY....
int ReadFromFile(GrItem grocery[], constint size) //granted friendship in GrItem.h
{
fstream inFile;
string filename;
int indexTotal = 0;
string foodname;
cout << "User, please enter the name of the textfile to read from." << endl;
cout << "Enter here: ";
cin >> filename;
inFile.open(filename, ios::in);
if (inFile.fail()) {
cout << "Error opening file! Exiting Program!" << endl;
exit(-1);
}
while (getline(inFile, grocery[indexTotal].name, '#') && indexTotal < size) //acts as a eof bit flag, will terminate loop if no more to read or size limit is reached
{
inFile >> grocery[indexTotal].price;
inFile >> grocery[indexTotal].quantity;
inFile.ignore(10000, '\n');
indexTotal++;
}
inFile.close();
cout << "Successfully read data from: " << filename << endl;
return indexTotal;
}
This right here ^^^^ will work fine if it is 10 items in the .txt file or less.
If more than 10, it crashes. I used similar (almost identical code) in another program and it handled more than what was allowed in the array just fine because it would terminate the loop early if an attempt was made to read more than allowed into the array of objects.
int ReadFromFile(GrItem grocery[], int size) //granted friendship in GrItem.h
{
fstream inFile;
string filename;
int indexTotal = 0;
string foodname;
cout << "User, please enter the name of the textfile to read from." << endl;
cout << "Enter here: ";
cin >> filename;
inFile.open(filename, ios::in);
if (inFile.fail()) {
cout << "Error opening file! Exiting Program!" << endl;
exit(-1);
}
while (getline(inFile, grocery[indexTotal].name, '#')) //acts as a eof bit flag, will terminate loop if no more to read
{
inFile >> grocery[indexTotal].price;
inFile >> grocery[indexTotal].quantity;
inFile.ignore(10000, '\n');
indexTotal++;
if (indexTotal >= 10) //will terminate loop if 10 or more items are read into the array
break;
}
inFile.close();
cout << "Successfully read data from: " << filename << endl;
return indexTotal;
}
This works... ^^^
Here below, is the sample output when using the .txt file in the original post with this function. ^^^^^^
Look at this snippet: while (getline(inFile, grocery[indexTotal].name, '#') && indexTotal < size)
You have your checks backward, you should be checking to insure indexTotal is < size before you try to use indexTotal as your array subscript. while ( indexTotal < size && getline(inFile, grocery[indexTotal].name, '#'))
Next look at this snippet:
1 2 3 4 5
#include "GrItem.h"
// function prototype
void PrintReceipt(GrItem[], int);
int main()
Where are your #includes of the standard include files? You should never rely on pre-processor "magic" to #include required #include files. Always #include all the required #include files in every file where they're required. Also your header file doesn't need most of those #includes, it should only have the #includes that are actually required for that file. And using the "using" statement in the global scope of a header is a very bad practice since it defeats the purpose using namespaces. You should use the scope resolution operator to properly scope your namespaces in header files, "std::string", etc.
I'll try out your code in a little bit here. I'll see if that works.
As for using the #include pre-processor directives I was taught that for efficiency sake you don't have to keep putting them into each and every file as long as you have them defined in a header file. So that you can just simply use the #include "ClassDefinition.h" to include all the other #include pre-processor directives in any other file.
As for using the #include pre-processor directives I was taught that for efficiency sake you don't have to keep putting them into each and every file as long as you have them defined in a header file.
Then you were taught wrong. You should only include the include files that are required for that particular file.
For example your include file only needs the <string> header file, and nothing more. When you add a bunch of "extra" includes those "extra" includes are always included when you include that header file which leads to extra compile time. As an example your class source file also only needs the <string> header but because you've included all of those extra headers in your header they will also be included in your class source file, which will increase compile times.
does it mean you don't like the idea of pre-compiled headers ?
They work only when you have one header that includes all the other headers that rarely change.
In theory that should actually speed up the build process.
does it mean you don't like the idea of pre-compiled headers ?
Pre-compiled headers are a different topic, but when used correctly they can be helpful when working with large projects. However, IMO, they are a waste of time for small projects such as this. The time to properly setup the pre-compiled header isn't worth the effort, IMO, for small project such as this.
Also you're not using pre-compiled headers so this is really not an issue for this program.