-1

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();}

        }

user
  • 934
  • 6
  • 17
Casey French
  • 139
  • 2
  • 5
  • 14
  • 3
    You can't recursively call `main` – cigien Apr 18 '20 at 20:47
  • 3
    `if(choice =1)` is always true because it is doing an assignment. Use `if (choice == 1)` instead. – 1201ProgramAlarm Apr 18 '20 at 20:48
  • 1
    As noted by @cigien, calling [`main` is undefined behavior](https://stackoverflow.com/questions/11349515/recursive-call-on-main-c). However, this likely isn't the source of whatever issue you're encountering since most implementations don't behave unreasonably when `main` is called. – Brian61354270 Apr 18 '20 at 20:48
  • 1
    Turn up your compiler warnings and flag them to be treated as fatal errors (because that's exactly what they are). They should be telling you several important things. – WhozCraig Apr 18 '20 at 20:52
  • @WhozCraig I will definitely do that when I get further along and feel like my program is close to alpha ready, but for now I allow it to compile with warnings because I am very new and want to write a little then test for proper function, I use things like un-used variables and functions instead as my TODO instead of commenting one out. this way when I correct it, I have only typed what I need and deleted nothing to show that it has been finished and corrected. idk if it actually saves time, but it feels like it does. – Casey French Apr 18 '20 at 21:43

1 Answers1

1

You're calling your functions recursively, while actually, what you want to do is calling them in a loop. Basic pattern:

while(true) {
  cin >> choice;
  switch(choice) {
    case 1: doSomething(); break;
    case 2: return 0;   // NOT return main()!
  }
  // no "if (choice == 1)" with recursion back into the same function.
  // Processing resumes at the top of the while loop. 
}

But more specifically to your question: return main(); calls another "instance" of the main menu, and then returns once that has ended. At that point it will return to main() (the calling function), which is exactly the behavior you are seeing: You need to quit the main menu, twice.

Forget about concurrency. There is none in your program.

Tfry
  • 194
  • 6