2D array of strings, "endl" problem, .txt file output

I am trying to output a two dimensional array of strings to a text file to be read next time the program starts. Some of the strings are more than one word (like "Chicken noodle soup"). The reading (using getline) of the .txt file seems to be working but the output to file does not.

The below code is pretty terrible as I tried to create both a file with the number of entries per row and a file with the actual entries of that row.

The program keeps crashing (without a debug problem with initial compiler) because of the line recipeFile<<endl; Using microsoft visual express C++ 2010 the program just stops working.

I know there must be a better way to do this...

I really don't want to work with vectors as everything in the program is in arrays.

I'm a beginning programer just trying to pick up C++ on my own. Any help would be appreciated. Thanks!!!

//Store recipe to file
void storeRecipestofile(string recipestoStore[ROWS][COLS]){
ofstream recipeFile;
ofstream NumberinRow;
recipeFile.open("Recipes.txt");
NumberinRow.open("Number_in_rows.txt");

for (int i=0;i<ROWS;i++){
if (recipestoStore[i][0].empty() == 1) {break;}

for (int j=0;j<COLS-1;j++) {
if (recipestoStore[i][j].empty() == 1){
NumberinRow<<j<<endl;
break;
}
recipeFile<<recipestoStore[i][j];

//THIS NEXT LINE CAUSES THE ERROR!!
recipeFile<<endl;
}
}

recipeFile.close();
NumberinRow.close();
}
Nothing is jumping out at me that would cause it to crash. The only thing I can think of is if ROWS and COLS had different values in the calling function.

A couple of things worth mentioning (neither of which would cause the crash):
1) In the following line, it's not clear why you're using COL-1 as your termination condition.
 
for (int j=0;j<COLS-1;j++) 


2) You're comparing a bool to an integer. You shouldn't mix types.
 
if (recipestoStore[i][0].empty() == 1) 

You should be comparing a bool to a bool.
 
if (recipestoStore[i][0].empty() == true)

or simply
 
if (recipestoStore[i][0].empty()) 


I know you don't want to hear it, but a recipe should be a vector of strings and collection of recipies should be a vector of recipes.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.

Thanks for the feedback, I think I will try to rewrite the code using vectors. Still not sure why it won't work but with vectors should have more success...
Topic archived. No new replies allowed.