title: How to do a function that will return me to main menu from any place of program?
body: <p>How to do a function that will clean screen and return me to a main menu of program even during entering elements of matrix. And how to disable pressing other keys except arrows and Enter during navigating the menu?</p>
  
  <p><strong>This is my C++ code:</strong></p>
  
  <pre><code>    while(running)
      {
          gotoXY(18,1);  cout &lt;&lt; "Main Menu";
          gotoXY(20,3);  cout &lt;&lt; " Enter matrix";
          gotoXY(20,4);  cout &lt;&lt; " Randomize matrix";
          gotoXY(20,5);  cout &lt;&lt; " Exit";
  
          system("pause&gt;nul"); // the &gt;nul bit causes it the print no message
  
          if(GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState(VK_RIGHT)) //down button pressed
              {
                  gotoXY(18,pos); cout &lt;&lt; "  ";
                  pos++;
                  gotoXY(18,pos); cout &lt;&lt; "-&gt;";
                  menu_item++;
                  if (pos == 6)
                      {
                          gotoXY(18,pos); cout &lt;&lt; "  ";
                          pos = 3;
                          gotoXY(18,3); cout &lt;&lt; "-&gt;";
                          menu_item = 0;
                      }
                  continue;
  
              }
  
          if(GetAsyncKeyState(VK_UP) || GetAsyncKeyState(VK_LEFT)) //up button pressed
              {
                  gotoXY(18,pos); cout &lt;&lt; "  ";
                  pos--;
                  gotoXY(18,pos); cout &lt;&lt; "-&gt;";
                  menu_item--;
                  if (pos == 2)
                      {
                          gotoXY(18,pos); cout &lt;&lt; "  ";
                          pos = 5;
                          gotoXY(18,5); cout &lt;&lt; "-&gt;";
                          menu_item = 2;
                      }
                  continue;
              }
  
  
  
          if(GetAsyncKeyState(VK_RETURN)){ // Enter key pressed
  
              switch(menu_item){
  
                  case 0: {
  
                      gotoXY(20,10);
                      int i, j, n;
                      double **a, *b;
                      cout &lt;&lt; "Enter NUMBER of equations: ";
                      cin &gt;&gt; n;
  
  
  
                      a = (double **)malloc(n*sizeof(double));
                      b = (double *)malloc(n*sizeof(double));
  
                      cout &lt;&lt; "Enter Matrix A\n";
                      for(i = 0; i &lt; n; i++)
                      {
                          a[i] = (double *)malloc(n*sizeof(double));
                          //Ввод a
                          for(j = 0; j &lt; n; j++)
                          {
                              cout &lt;&lt;"a["&lt;&lt; i + 1 &lt;&lt; "][" &lt;&lt; j + 1 &lt;&lt; "] = ";
                              //cin &gt;&gt;a[i][j];
                              a[i][j] = proverkafloat();
                          }
                      }
  
                      cout &lt;&lt; "\tSee input\r\n";
                      cout &lt;&lt; "Matrix A:\r\n";
                      for(i = 0; i &lt; n; i++)
                      {
                          cout &lt;&lt; "|\t";
                          ShowVector(n, a[i]);
                      };
  
  
                      cout &lt;&lt; endl;
                      cout &lt;&lt; "Enter Vector B\n";
                      for(i = 0; i &lt; n; i++)
                      {
                          cout &lt;&lt; "b[" &lt;&lt; i + 1 &lt;&lt; "] = ";
                          cin &gt;&gt; b[i]
                      }
  
                      cout &lt;&lt; "\n\n";
                      cout &lt;&lt; "\tSee input\r\n";
                      cout &lt;&lt; "\n";
                      cout &lt;&lt; "Vector B:\r\n";
                      cout &lt;&lt; "|\t";
                      ShowVector(n, b);
  
  
                      system("pause");
                      system("CLS");
                      gotoXY(18,pos); cout &lt;&lt; "-&gt;";
                      break;
                  }
  
                  case 1:
                    {
                      gotoXY(20,10);
                      int i, j;
                      double **a, *b, n;
                      cout &lt;&lt; "Enter NUMBER of equations: ";
                      cin &gt;&gt; n;
  
                      srand(time(0));
                      a = (double **)malloc(n*sizeof(double));
                      b = (double *)malloc(n*sizeof(double));
                      //Randomizing Matrix A
                      for(i = 0; i &lt; n; i++)
                      {
                          a[i] = (double *)malloc(n*sizeof(double));
                          //Ввод a
                          for(j = 0; j &lt; n; j++)
                          {
                            a[i][j] = (double)(rand() % 100 + (-50))/((double)(rand() % 100 + (-50)));
                          }
                      }
  
                      cout &lt;&lt; "\tSee input\r\n";
                      cout &lt;&lt; "Matrix A:\r\n";
                      for(i = 0; i &lt; n; i++)
                      {
                          cout &lt;&lt; "|\t";
                          ShowVector(n, a[i]);
                      };
  
  
                      //Randomizing Vector B
                      for(i = 0; i &lt; n; i++)
                      {
  
                          b[i] = (double)(rand() % 100 + (-50))/((double)(rand() % 100 + (-50)));
                      }
  
                      cout &lt;&lt; "\n\n";
                      cout &lt;&lt; "\tSee input\r\n";
                      cout &lt;&lt; "\n";
                      cout &lt;&lt; "Vector B:\r\n";
                      cout &lt;&lt; "|\t";
                      ShowVector(n, b);
  
  
                      system("pause");
                      system("CLS");
                      gotoXY(18,pos); cout &lt;&lt; "-&gt;";
                      break;
                    }
  
                  case 2:
                          {
                              gotoXY(20,10);
                              cout &lt;&lt; "The program has now terminated!!";
                              running = false;
                              getch();
                          }
  
              }
  
          }
  
      }
  
      gotoXY(20,21);
      return 0;
  }
  
  void gotoXY(int x, int y)
      {
          CursorPosition.X = x;
          CursorPosition.Y = y;
          SetConsoleCursorPosition(console,CursorPosition);
      }
  
  void ShowVector(int n, double * vec)
      {
          for(int i = 0; i &lt; n; i++)
          cout &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; vec[i] &lt;&lt; "  ";
          cout &lt;&lt; "\t";
          cout &lt;&lt; "|";
          cout &lt;&lt; "\r\n";
      }
  </code></pre>