Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can I make it return to the first menu?

+4 votes
asked May 16, 2023 by Kedra (160 points)

original version:

https://learn.onlinegdb.com/switch_case_statement_c

I just started learning c language. While doing some exercise I wanted to add something new to the original version. I changed the font where I added something new. 

//simple menu driven program for addition, subtraction and multiplication using switch case
#include<stdio.h>
int main()
{
        int a,b,ch1,c,ch2;   // declaration of variables of integer type
        printf("\n\t Menu");
        printf("\n\t 1. Addition");
        printf("\n\t 2. Subtraction");
        printf("\n\t 3. Multiplication");
        printf("\n\t 0. End the program");// added a new option, which is 0, that will end the program after confirmation.
        printf("\n\n\t Enter Your Choice: ");
        scanf("%d", &ch1);       // getting value from user for ch
        if(ch1=0)
        {
            printf("Are you sure you want to exit?\n If yes, press 0. If not, press any other number.");
            scanf("%d", &ch2); //getting value from user for ch2
            if(ch2=0)
            {
                printf("End the program")
                break;
            }
            else
            {
                //this is the part where I don't know. what should I add to make it end the question and return back to the original menu? 
            }
        }

        if(ch1<=3 && ch1>-1)       // if the value of ch is not between 0 to 3 it will print the default case
        {
                printf("Enter two numbers: ");
                scanf("%d %d",&a,&b);

        }

        switch(ch1)
        {
                
                case 1: // code to be executed if "ch1=1"
                c=a+b;
                printf("\n Addition: %d",c);
                break;

                case 2: // code to be executed if "ch1=2"
                c=a-b;
                printf("\n Subtraction: %d",c);
                break;

                case 3: // code to be executed if "ch1=3"
                c=a*b;
                printf("\n Multiplication: %d",c);
                break;

                default:        // code to be executed if ch1 doesn't match with any cases
                printf("\n Invalid Choice");
                break;
        }
    return 0;
}

2 Answers

0 votes
answered Jun 8, 2023 by rohan ag (1,310 points)
#include<stdio.h>
int main()
{
        int a,b,ch1,c,ch2;   // declaration of variables of integer type
        while(1)
        {
        printf("\n\t Menu");
        printf("\n\t 1. Addition");
        printf("\n\t 2. Subtraction");
        printf("\n\t 3. Multiplication");
        printf("\n\t 0. End the program");// added a new option, which is 0, that will end the program after confirmation.
        printf("\n\n\t Enter Your Choice: ");
        scanf("%d", &ch1);       // getting value from user for ch
        
        if(ch1>=1 && ch1<=3)       // if the value of ch is not between 0 to 3 it will print the default case
        {
                printf("Enter two numbers: ");
                scanf("%d %d",&a,&b);

        

        switch(ch1)
        {
                
                case 1: // code to be executed if "ch1=1"
                c=a+b;
                printf("\n Addition: %d",c);
                break;

                case 2: // code to be executed if "ch1=2"
                c=a-b;
                printf("\n Subtraction: %d",c);
                break;

                case 3: // code to be executed if "ch1=3"
                c=a*b;
                printf("\n Multiplication: %d",c);
                break;

                default:        // code to be executed if ch1 doesn't match with any cases
                printf("\n Invalid Choice");
                break;
        }
        }
        }
    return 0;
}
+1 vote
answered Jun 9, 2023 by Peter Minarik (86,180 points)

First of all

if(ch1=0)

does not do what you think it does.

It does not check if the value of ch1 is 0, but it assigns the value 0 to ch1. Then checks, if the expression is true. The expression is the value of ch1 (after assignment), that is, 0. An integral number is interpreted as true, if the value is not 0. But since it is 0, if (ch1 = 0) evaluates to false all the time.

That being said, the right code is checking for equality, not assigning value, which is written using double equal signs:

if (ch1 == 0)

So, your question: how to stop execution at a given point and return back to the original menu? Well, there's no original menu to return to in a sense that the program starts, asks a question then does something and quits.

What you could do is create a loop around the main code: asking for an option, asking for the numbers, and doing arithmetics with them:

#include<stdio.h>

int main()
{
    int a, b, ch, c;   // declaration of variables of integer type
    do
    {
        printf("\n\t Menu");
        printf("\n\t ====")
        printf("\n\t 0. End the program");// added a new option, which is 0, that will end the program after confirmation.
        printf("\n\t 1. Addition");
        printf("\n\t 2. Subtraction");
        printf("\n\t 3. Multiplication");
        printf("\n\n\t Enter Your Choice: ");
        scanf("%d", &ch);       // getting value from user for ch
        
        if (ch <= 3 && ch > 0)       // if the value of ch is not between 0 to 3 it will print the default case
        {
            printf("Enter two numbers: ");
            scanf("%d %d",&a,&b);
        }
    
        switch(ch)
        {
            case 0:
                printf("Are you sure you want to exit?\n If yes, press 0. If not, press any other number.");
                scanf("%d", &ch); //getting value from user for ch2
                if (ch == 0)
                {
                    printf("End the program");
                }
                break;

            case 1: // code to be executed if "ch=1"
                c = a + b;
                printf("\n Addition: %d",c);
                break;

            case 2: // code to be executed if "ch=2"
                c = a - b;
                printf("\n Subtraction: %d", c);
                break;

            case 3: // code to be executed if "ch=3"
                c = a * b;
                printf("\n Multiplication: %d", c);
                break;

            default: // code to be executed if ch doesn't match with any cases
                printf("\n Invalid Choice");
                break;
        }
    } while (ch != 0);
    
    return 0;
}

The added loop is marked with yellow.

Please note, that you do not need to change when numbers need to be entered for arithmetic operations (marked with purple) when you add your quit option.

Your quit logic is best handled inside the switch case, so I moved it there and marked it with turquoise. Please notice that you do not need ch2 either, you can just use the original ch when you ask for confirmation. If the user enters 0, that is the same as the original menu item 0, so we could store it in the same ch variable.

I hope this helps.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...