Im creating a program in Dev C++ and the program has a switch statement in it to take the user to a differ 'page'. For example the user presses 1 and it takes them to a page that deals with orders etc. E.g.

switch(mainmenu)
{
        case '1':
           Menu1();
           break;
}

Then when Menu1 is displayed it clears the screen with system("CLS"). How can, once the user has finished using that page, return back to the main menu?

Well, it's a little tough to help much due to a lack of context. I think it would be helpful if you posted a little bit more code so that we can see your overall context better.

Generally though, if you need a section of code to repeat you'd use a loop. For a menu, the menu display, input, and processing would all be part of the loop.

Here is the code for my main menu page and the first page it links to:

#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>

using namespace std;

char option;

struct product
{
       string ID;
       string name;
       string price;
       string quantity;
};
int main(int argc, char *argv[])
{
    cout<<"--------------Drinks Program--------------\n";
    cout<<"[1]\t Order a Drink"<<endl;
    cout<<"\n\n Select an option and hit Enter to continue to that page: ";
    char mainmenu;
    
    cin>>mainmenu;
    
    switch(mainmenu)
    {
                    case '1':
                         Menu1();
                         break;
    } while (mainmenu != 'x' && mainmenu != 'X');
    
    return EXIT_SUCCESS;
}
void Menu1()
{
     int myPointer;
     system("CLS");
     
     do 
     {
     product products;
     option=getch();
     cout<<"--------------Order a Drink--------------\n"<<endl;      
     cout<<"Enter Product ID: ";
     cin>>products.ID;
     }
     while(option != 'x' && option != 'X');
}

Well, the way I normally do it is I would wrap your "main()" into a function like main2() and then at the end menu1() make a call to main2(). Then in main() just call main2() and it will work like it should. I'm not the best with programing but it has worked for me thus far. I'm sure there are other ways but that's just my way. Hope this helps.

int main2()//this would be your starting menu
{
cout<<"--------------Drinks Program--------------\n";
cout<<"[1]\t Order a Drink"<<endl;
cout<<"\n\n Select an option and hit Enter to continue to that page: ";
char mainmenu;
 

cin>>mainmenu; 

switch(mainmenu)
{
case '1':
Menu1();
break;

} while (mainmenu != 'x' && mainmenu != 'X');

 
return EXIT_SUCCESS;

}
int main()//this would be just the call to main2
{
main2();
return 0;
void Menu1()
{
     int myPointer;
     system("CLS");
     
     do 
     {
     product products;
     option=getch();
     cout<<"--------------Order a Drink--------------\n"<<endl;      
     cout<<"Enter Product ID: ";
     cin>>products.ID;
     }
     while(option != 'x' && option != 'X');
main2()//notice this call to go to beginning of program(put it wherever)
}
commented: Absolutely not, your main() is not intended to be recursive and never should be made such. -1

A simple idea that i thought would be perfect but the problem is if my main is directing the user to main1 then main1 must be above it in the code so it knows it has been declared, however if i then try to direct from main1 back to main then it doesnt know what main is as it hasnt got far enough down the code to know that it has been declared. Thanks anyway though!

Well, the way I normally do it is I would wrap your "main()" into a function like main2() and then at the end menu1() make a call to main2(). Then in main() just call main2() and it will work like it should. I'm not the best with programing but it has worked for me thus far. I'm sure there are other ways but that's just my way. Hope this helps.

int main2()//this would be your starting menu
{
cout<<"--------------Drinks Program--------------\n";
cout<<"[1]\t Order a Drink"<<endl;
cout<<"\n\n Select an option and hit Enter to continue to that page: ";
char mainmenu;
 

cin>>mainmenu; 

switch(mainmenu)
{
case '1':
Menu1();
break;

} while (mainmenu != 'x' && mainmenu != 'X');

 
return EXIT_SUCCESS;

}
int main()//this would be just the call to main2
{
main2();
return 0;
void Menu1()
{
     int myPointer;
     system("CLS");
     
     do 
     {
     product products;
     option=getch();
     cout<<"--------------Order a Drink--------------\n"<<endl;      
     cout<<"Enter Product ID: ";
     cin>>products.ID;
     }
     while(option != 'x' && option != 'X');
main2()//notice this call to go to beginning of program(put it wherever)
}

That is not a good idea. This is a very bad example of what is called "recursion". The main() function is NOT intended to be recursive and never should be made such. Basically what this does is cause several instances of both Menu1() and main2() to be running at the same time. Eventually, this will use up all of your system's memory and either cause major slow downs or a crash. A proper loop is a much better option.

int main(int argc, char *argv[])
{
    cout<<"--------------Drinks Program--------------\n";
    cout<<"[1]\t Order a Drink"<<endl;
    cout<<"\n\n Select an option and hit Enter to continue to that page: ";
    char mainmenu;
 
    cin>>mainmenu;
 
    switch(mainmenu)
    {
                    case '1':
                         Menu1();
                         break;
    } while (mainmenu != 'x' && mainmenu != 'X');
 
    return EXIT_SUCCESS;
}

It looks like you tried to use a do loop. That's not a bad idea. There's just one problem... You forgot the "do". As a result, you've written an infinite loop that does absolutely nothing. Your loop needs to start on either Line 3 or Line 4 depending on which output statements you would like repeated.

A simple idea that i thought would be perfect but the problem is if my main is directing the user to main1 then main1 must be above it in the code so it knows it has been declared, however if i then try to direct from main1 back to main then it doesnt know what main is as it hasnt got far enough down the code to know that it has been declared. Thanks anyway though!

I don't think you understand how declarations work. That is actually not a problem if you use proper prototypes.

However, that being said, it is a really stupid way to write the program for the reasons I mentioned earlier.

If i insert a do into my code where you suggested, i get an error saying a while is expected on the line below it.

That's because you messed up your braces. Your while isn't associated to the do because you don't have your braces right.

Based on your earlier post, you probably did this:

do
  /* code here
     code here
  */
  switch (selector) {
    //case
    //case
  } while (condition);

Which is nowhere near correct. You need this:

do {
  /* code here
     code here
  */
  switch (selector) {
    //case
    //case
  }

} while (condition);

Notice the extra set of braces.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.