I'm having a really weird problem I can't seem to nail down. I need to provide a way to step back from one menu to the previous menu, my program does this, however once I have entered the secondary menu which I have programmed as a function when I step back to the previous menu in the main function it requires that you select the quit option in the first menu twice before the program actually returns 0 and prompts to close the console. I understand that my code is messy right now, because I am in the middle of putting things together and I am still learning. It does not have any critical errors (compiles and runs in my intended output excepting the issue stated), It does contain a few warnings (most of which will be corrected when I finish it) I have not tried using global variables because everyone seems to hate them, but I am wondering if global variables are required for concurrency between methods when not using pointers (something I have not learned how to use yet). I am only asking for an explaination of what I have done wrong and what I can do to fix it.
My code for the main function.
#include <iostream>
#include <cstdlib>
using namespace std;
int countBySheet();
int main()
{
int choice;
cout << "Express estimator" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "1. Count by Sheet (eg. Lighting, Service, Site Lighting, Power)" << endl;
cout << "2. Count by Category (eg. Building, HVAC, Large Circuits, Service, Site Lighting)" << endl;
cout << "3. Quit Program" << endl;
cin >> choice;
switch(choice) {
case 1: cout << "Count by Sheet" << endl;
cout << " " << endl;
cout << choice << " is the value of the CHOICE variable" << endl;
cout << " " << endl;
countBySheet();
break;
case 2: cout << "Count by Category" << endl;
cout << " " << endl;
cout << choice << " is the value of the CHOICE variable" << endl;
cout << " " << endl;
//create count by category function
break;
case 3: cout << "Quit program" << endl;
return 0;
}
if(choice =1){ main();}
else if (choice =2) {main();}
else if (choice=3) {return 3;}
// I originally had one else if statement and allowed the console to terminate from the return statement included in case 3, this produced the explained effect after I chose case 1 or case 2 first, I then added the second else if statement with a return 0 and the explained effect continued, I changed the return statement to 3 to see if that would make a difference, it does not.
}````
My code for the Secondary function.
#include <iostream>
#include <cstdlib>
using namespace std;
int main();
int countBySheet()
{
int choice;
cout << "By Sheet" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "1. Lighting Plan" << endl;
cout << "2. Power Plan" << endl;
cout << "3. Service plan" << endl;
cout << "4. Site plan" << endl;
cout << "5. Back" << endl;
cin >> choice;
switch(choice) {
case 1: cout << "Lighting Plan" << endl;
//create count by sheet function
break;
case 2: cout << "Power Plan" << endl;
//create count by category function
break;
case 3: cout << "Service Plan" << endl;
break;
case 4: cout << "Site Plan" << endl;
break;
case 5: cout << "Back" << endl;
return main();
}
if(choice =1){ countBySheet();}
else if (choice =2) {countBySheet();}
}