1. figured it out - the loop always evaluates to true, even if you
enter in one of the options - if choice is 8, the part about
choice != 9 evaluates to true, meaning the statement is true.
Same the other way around. To fix, use && instead of ||

2. I still see no reason for the loop not to terminate. Putting return 0 in the main switch cases works, although not the proper way to do it, I guess it'll have to do for now cause I'm completely stumped

3. No, you misunderstand. When you use logical OR if either of the statements are true, the whole statement is true and evaluation is done.

If the choice is 8. choice != 9 TRUE

If the choice is 9. choice != 8 TRUE

Using logical AND both must be true in order to continue. If choice != 8 && choice != 9, the loop repeats. If the user types either 8 or 9, then the loop breaks correctly.

4. I see, Richie T got a post in before me and I didn't notice it. I understand my error but this brings good news and bad news.

Good news is that my loop now terminates when its supposed to, bad news is that my original problem that caused me to create this thread is back. Well, it was never gone because of my mistake.

Here is my code so far:

Code:
```// SAVE STOCK DETAILS TO A TEXT FILE
void save_items( int numitems)
{
bool stayinloop = true;

cout << "\nPlease enter the desired save name: ";
cin >> filename;     //char filename [12];

while (stayinloop == true)
{
cout << "\n\t\tSaving as " << filename <<  " with " << numitems-1
<< " items\n\n" << "Do you wish to continue (Y/N)? ";
cin >> ans;
int i = 1;
FILE *out_file;

switch (tolower(ans[0]))
{
case 'y':
cout << "\n\n\tSaving as " << filename << ".......";
out_file=fopen(filename,"w");

do {
fprintf(out_file,"%7d %25s %25s %7d %7.2f \n", slist[i].stocknumber,
slist[i].description,   slist[i].supplier, slist[i].quantity,
slist[i].price);
i++;
} while (i <= numitems-1);

cout <<"\n\n\tSaved a total of " << i-1 << " items\n\n\n";
fclose (out_file);

cout << "Program Saved";
stayinloop = false;
break;

case 'n':
cout << "Save Cancelled";
stayinloop = false;
break;

default: //default stuff
}
}
}

int main(int argc, char* argv[])
{
int choice;
int numitems=0;

listitems(numitems);
waitforkey();

do {
cout << "        1.  Add item\n";
cout << "        2.  Update an Item\n";

cout << "        8.  Exit without Saving Changes\n";
cout << "        9.  Save changes and Exit\n";
cout << "        ================\n";
cout << "        Press appropriate key to select > ";
cin >> choice;

switch (choice){

case 1: //case 1 stuff

case 2: //case 2 stuff

case 8:
cout <<("\n\t\t PROGRAM NOT SAVED\n");
waitforkey();
break;

case 9:
cout <<("\n\t\t SAVING CHANGES\n");
save_items( numitems);
waitforkey();
break;

default: // default stuff
}}
while (choice != 8 && choice != 9);
return 0;
}```
Basically, what I'm wanting to do is that when the user chooses option 9 to save, enters a filename, then gets the option to continue(Y/N). When the user selects yes, it saves and the program ends. This all works.

When the user is asked if they want to continue or not and they select no, I want it to display "Save Cancelled" and then show the main menu again, as in run int main again. So far when no is selected, it displays save cancelled and the program closes. How do I get around this?

Seeing as my problem is still there, I think the bool and stayinloop variables can be removed and my original save code in my first post used again. But the more pressing matter is the returning to main. Any suggestions?

5. >When the user is asked if they want to continue or not and they select no, I want it to
>display "Save Cancelled" and then show the main menu again, as in run int main again.
>So far when no is selected, it displays save cancelled and the program closes. How do I
>get around this?
I don't think you can. Or at least you shouldn't. Doing so is breaking your own logic. One way though, is to pass a reference to the choice variable in your save functions. If the user cancels anything, set choice to zero and return. In main, the while loop will continue and the user can make another choice.

6. I'm not following you I'm afraid, how would I go about doing this?

*edit*

I think I get it now, I'll give it a try and see what happens

7. Okay, here is your function prototype for save options:
Code:
`void save_items( int  numitems, int& choice);`
Now your function expects a reference to an int. When you call this function, pass in numitems as well as the user's choice variable.

If the user decides he wants to cancel a save, you must handle this choice properly. Take your choice variable and
Code:
```choice = 0; // set it to zero
return;```
Now choice equals zero in main. Since the while condition is still true at this point, the menu gets redisplayed and the user gets to make another choice.

8. thank you very much, int& choice was what I was missing. Working perfectly now One small question though, why int& and not just int?

9. Because you would be passing the integer by value, which means that a copy of choice is made. When the function returns, the copy is destroyed and it will have no effect on the code in main.

Passing the integer by reference circumvents these problems because you actually pass the integer's memory address.

10. I still think you really should use C++ file IO! It's really easy I think!! Here's an example:

Code:
```#include <iostream>
#include <fstream>

using namespace std;

int main( void )
{
int    MyNum    = 42;
char   MyChar   = 'x';

ofstream ThisStreamSendsInfoOutToAFile ( "MyFileName.MyFileExtension" );
// Ok, you create an ofstream instance thing, which I called: ThisStreamSendsInfoOutToAFile
// This basically acts like cout and such. The 'MyFileExtension' can be anything -
// .txt, .bat etc.

ThisStreamSendsInfoOutToAFile<< "This is outputting text " << endl << "To a file!!"
<< endl << MyNum << ends << MyChar;
// As I said, ThisStreamSendsInfoOutToAFile, acts like cout. You can call it anything
// you want. I would typically call it out or something, but that is immiterial.
// This sends:

// This is outputting text
// To a File!!
// 42 x

// to a file

ThisStreamSendsInfoOutToAFile.close();
// This ensures that the file you opened is now closed so that you won't have any problems
// onening it after you are finished using it.
// That is the basics of the ostream-ing to a file.

// The opposite, writing to a file is much like cin.

ifstream ThisWritesToAFile ( "WriteToThisFile.Something" );
// This opens a file, which has already been created, called 'WriteToThisFile.Something',
// where the .Something could be .txt, .bat etc. Just like in C style file IO. If you
// want to read info from the file you can do it like so:

ThisWritesToAFile>> MyChar;

// or you could do it in a loop:

string MyString;

while ( SetParametersHere )
{
ThisWritesToAFile>> MyChar;
MyString += MyChar;
}

// Then close it again!

ThisWritesToAFile.close();

return 0;
}```