0

I've only just started to learn C++, I'm currently trying to build a program that will run several programs inside itself, all of which can be accessed by a main menu, I've managed with some help to make a multiplication chart come up as my first program the user can choose, but I can't really figure out how to get the program to clear the screen and bring the main menu back up, the issue is that what I've tried so far: getchar(), return 0;, system("PAUSE") only halt the for loop that I have after one line has been printed, and in some cases you have to hit enter every single line. So is there a way I could get the entire chart to print and have it sit on screen until the user closes it with a specific key, that key also clearing the screen and bringing my main menu back up? Thanks in advance, I've done some searching but I'm rather confused at the moment.

Here's what I've written so far, this is my menu and my charts loop...

printf("\n******************************************************");
printf("\n******************************************************");
printf("\n**         Hello and welcome to my program!         **");
printf("\n** Please select the program you would like to run! **");
printf("\n******************************************************");
printf("\n**                 - 1. Multiplication table        **");
printf("\n**                 - 2. Program B  -                **");
printf("\n**                 - 3. Program C  -                **");
printf("\n**                 - 4. Program D  -                **");
printf("\n******************************************************");
printf("\n******************************************************");
printf("\n ");
scanf("%d", &selection);
system("CLS");

switch (selection) {

    case 1:  
        int i, j;    

        printf ("   |");    
        for (i = 1; i <= 10; ++i)    
            printf ("%#3d   ", i);    
        printf ("\n");    

        for (i = 1; i < 64; ++i)    
            printf ("-");    
        printf ("\n");    

        for (i = 1; i <= 10; ++i) {    
            printf ("%#2d |", i);    
            for (j = 1; j <= 10; ++j)    
                printf ("%#3d   ", i * j);    
            printf ("\n");
        }

        break;
braX
  • 11,506
  • 5
  • 20
  • 33
Nism
  • 1
  • 2

1 Answers1

0

Many questions here and no C++. Answering in C.

Clearing the screen is beaten to death elsewhere.

As for looping, put your code above into a while loop and add a fifth selection: exit.

bool done = false;
while(!done)
{
    printf("\n******************************************************");
    printf("\n******************************************************");
    printf("\n**         Hello and welcome to my program!         **");
    printf("\n** Please select the program you would like to run! **");
    printf("\n******************************************************");
    printf("\n**                 - 1. Multiplication table        **");
    printf("\n**                 - 2. Program B  -                **");
    printf("\n**                 - 3. Program C  -                **");
    printf("\n**                 - 4. Program D  -                **");
    printf("\n**                 - 5. Exit       -                **");
    printf("\n******************************************************");
    printf("\n******************************************************");
    printf("\n ");
    scanf("%d", &selection);
    system("CLS");

    switch (selection)

    {

     case 1:
          {    
            int i, j;    

              printf ("   |");    
                for (i = 1; i <= 10; ++i)    
                  printf ("%#3d   ", i);    
                  printf ("\n");    

                  for (i = 1; i < 64; ++i)    
                  printf ("-");    
                  printf ("\n");    

                for (i = 1; i <= 10; ++i){    
                    printf ("%#2d |", i);    
                    for (j = 1; j <= 10; ++j)    
                    printf ("%#3d   ", i * j);    
                    printf ("\n");

               }

               break;
     case 5:
         done = true;
         break; 
}
Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thanks a-lot that got the main menu coming up after the table was printed, now i just need to get the menu to only print after a certain key has been pressed when the table is finished. Thank you very much :D – Nism Jun 04 '15 at 01:31