-6

I have these functions:

read(){
}

write(){
}

main(){
    cout << "This is a menu so you can choose what to do";
}

Its actualy more complicated than that but that example will do fine for you to help me.

I just want to be able to for example write, then go back to the menu, then choose to read etc

I cant do :

read(){
 main();
}

main(){
    read();
}

because main was not declared before read, so then I move main above read and read was not declared before main.

Exactly this program is an agenda, so that I can creat schedules and manage events (write) and just see my calendar with all the events or just see my schedules (read). I want to be able to go from main to read, then to main again then to write you know

I think I could separate these functions into different files but I dont know how to call a function from another file.

Any help?

  • Never call `main()` recursively in any manner! Use a loop instead. – user0042 Nov 06 '17 at 21:05
  • You can't call `main` anyway, so that is a non-starter. – PaulMcKenzie Nov 06 '17 at 21:06
  • Don't call `main`. If you ever feel you need to do that, then you are *doing it wrong*. – Jesper Juhl Nov 06 '17 at 21:06
  • 1
    disagree (mostly because never is such a strong word). Regardless, you're probably better off with a loop inside main that displays whatever (main menu (don't call this main()), or read()). Also to the main question, you prototype the function before main, then you can call it inside main. – xyious Nov 06 '17 at 21:07
  • 1
    @xyious Calling `main` is UB in C++. Thus you can *never* do it. – Justin Nov 06 '17 at 21:08
  • 2
    @xyious [Never *is* the correct word to use](https://stackoverflow.com/questions/27250120/how-do-i-call-main-in-my-main-cpp-file-from-a-seperate-cpp-file) – PaulMcKenzie Nov 06 '17 at 21:09
  • I apologize.... It's legal and valid in C, so I just assumed.... https://stackoverflow.com/questions/4238179/calling-main-in-main-in-c – xyious Nov 06 '17 at 21:11
  • What happened to just using conditionals, while loops, etc.? Why this playing games with calling`main()`? Am I missing something simple here? How about just `int main() { startProgram(); }` and use `startProgram` as your `main`, if you can't figure out how to use `while` or `for` loops. – PaulMcKenzie Nov 06 '17 at 21:12

3 Answers3

2

Here's another alternative:

int main(void)
{
   bool call_read = true;
   while (true)
   {
      if (call_read)
      {
         read();
      }
      else
      {
         write();
      }
      call_read = !call_read;
    }
   return 0;
}

In the above example, a bool variable is used to alternate between the read and write functions.

A nice feature of this method is that the functions called don't need to know about each other.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Another positive of this solution is avoiding the (possibly infinite) recursion. However, unless the functions called have observable side effects or somehow cause the loop to terminate, then you have an "infinite loop without observable side effects" and that is in fact UB in C++. – Jesper Juhl Nov 06 '17 at 21:31
0

ok, calling main is not a good idea, but I guess the question is more about how having 2 functions calling each other.

you need to forward declare at least one:

int write(); //forward declare

int read() {
   return write(); // use write which is still forward declared for now
}

int write() { // now defining write
   return read();
}

obviously this sample code is meaning less (infinite loop), but it illustrates how to do that.

OznOg
  • 4,440
  • 2
  • 26
  • 35
0

A good way to write menus is to use a while loop in main and a switch statement. Assuming the read and write functions do not branch somewhere else, when they complete, they return to their caller, main(). The while loop ensures that the menu does not end until the user is done.

int main()
{
    char choice = '\n';
    while (choice != 'q') 
    {
    // Display menu options
       cout << "r: Read" << endl << "w: Write" << endl << "Enter a choice: ";
       cin >> choice;

    // Implement menu interface
        switch (choice)
        {
        case 'r':
            read();
            break;
        case 'w':
            write();
            break;
        case 'q':
            cout << "Quitting the menu. Bye!" << endl;
            break;
        default:
            cout << "Invalid choice!" << endl;
            break;
        } //end switch
     } // end while

   return 0;
} // end main
Elle
  • 17
  • 2
  • 5