I've got a problem and I'm wondering can this be done.
I've written a program that has a menu list and one of the options is to quit and save. the save option is contained in a VOID and the menu part is in the main.
When you select the quit option, the save part comes up and asks for a filename. Once the filename is entered, I've got a wish to continue option. When the user selects Yes, it will continue to save, when No, I want it to go back to the menu (basically go back to the main code), not just ask for the filename again.
Here's a cut down version of my current code, I've not got the menu void put in because it didn't work.
I included the main part of my code to help get it into context. The code currently just reruns the save_items part when No is selected and asks for a filename again. How would I get that to run the main part again?Code:// SAVE STOCK DETAILS TO A TEXT FILE
void save_items( int numitems)
{
//char filename [12];
cout << "\nPlease enter the desired save name: ";
cin >> filename;
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;
do {
switch (tolower(ans[0])){
case 'n':
save_items(numitems);
break;
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);
break;
}
} while (tolower(ans[0])!='n');
}
int main(int argc, char* argv[])
{
char choice[80];
int numitems=0;
char searchchoice[80];
int i;
do {
system("cls");
cout << "\n\t\t ======MENU======\n";
cout << "\t\t 1. Search for an Item\n";
cout << "\t\t Q. To quit program\n";
cout << "\n\t\t ================\n";
cout << "\n\t\t Press appropriate key to select > ";
cin >> choice;
else if (!strcmp(choice, "1"))
{
do{
system("cls");
cout << "\n\t\t ======SEARCH \n";
cout << "\t\t 1. Search by stocknumber\n";
cout << "\t\t Q. End Search\n";
cout << "\n Press appropriate key to select > ";
cin >> searchchoice;
if (!strcmp(searchchoice, "1"))
{detailitem(numitems);
waitforkey();
}
else if (!strcmp(searchchoice, "q"))
{cout <<("\n\n\t\t SEARCH ENDED\n");
cout <<("\n\t\t Returning to main menu.\n");
waitforkey();
}
else if (!strcmp(searchchoice, "Q"))
{cout <<("\n\n\t\t SEARCH ENDED\n");
cout <<("\n\t\t Returning to main menu.\n");
waitforkey();
}
else {cout <<("\t\t PLEASE TRY AGAIN\n");
waitforkey();
}
}
while (tolower(searchchoice[0]) != 'q' );
}
else if (!strcmp(choice, "q"))
{
cout <<("\t\t QUIT PROGRAM\n");
save_items( numitems);
cout <<("\t\t PROGRAM COMPLETED");
waitforkey();
}
else if (!strcmp(choice, "Q"))
{
cout <<("\t\t QUIT PROGRAM\n");
save_items( numitems);
cout <<("\t\t PROGRAM COMPLETED");
waitforkey();
}
else {
cout <<("\t\t PLEASE TRY AGAIN\n");
waitforkey();
}
}
while (tolower(choice[0]) != 'q');
return 0;
}