Pages

Thursday, March 10, 2011

Nested Switch Statment


Nested switch
            A switch statement inside a switch statement. Just like nested if and so can switch be nested.

Sample Problem:
            JBE Fast Food Restaurant hired you to write a program to cater their needs for the cashier. Your program will display the value meal menu for the following food products:

                        <B>urger Meal
                        <C>hicken Meal
                        <P>asta Meal

            The user will choose from the selection menu and enter the corresponding key for the value meal. Each value meal will have another set of menu (sub menu) that will be displayed. The user again will choose from the sub menu and enter the corresponding key for each sub menu. These sub menus are:


Burger Meal                                                    Sub Menus
                                                           
                        Regular Burger           Cheese Burger            Quarter Pound Burger

Price                       $2.00                            $2.50                               $3.00


Chicken Meal             
Fried Chicken             Grilled Chicken                Chef’s Chicken
Price                       $4.00                            $5.00                               $6.00

Pasta Meal                 
Spaghetti de Italiano   Lasagna Supreme           Macaroni Special
Price                       $3.00                            $4.00                               $3.50


            After all the menu and sub menu has been entered, the program will ask the user to enter the no. of orders. The program will then compute for the total amount to paid by the customer and display the summary of order. The program will ask again for the customer cash and compute for the customer change.


Sample Program:

#include<iostream>
using namespace std;

int main()
{
    char vm, vmtype;
    string vmname;
    int order;
    float tprice, price, cash, change;
    cout<<"Welcome to JBE Fast Food Restaurant"<<endl<<endl
        <<"Please choose from the following Value Meal:"<<endl
        <<"<B>urger Meal"<<endl
        <<"<C>hicken Meal"<<endl
        <<"<P>asta Meal"<<endl
        <<"Value Meal -> ";
    cin>>vm;

    switch(vm)
    {
        case 'b':   cout<<endl<<"Please choose Value Meal for Burger"<<endl
                        <<"<R>egular Burger"<<endl
                        <<"<C>heese Burger"<<endl
                        <<"<Q>uarter Pound Burger"<<endl
                        <<"Burger Meal -> ";
                        cin>>vmtype;

                    switch(vmtype)

                    {
                        case 'r':   vmname = "Regular Burger";
                                    price = 2.00;
                                    break;
                        case 'c':   vmname = "Cheese Burger";
                                    price = 2.50;
                                    break;
                        case 'q':   vmname = "Quarter Pound Burger";
                                    price = 3.00;
                                    break;
                        default:    cout<<"Invalid input, please come again!"<<endl;
                                     return 0;
                                    break;
                    }
                    break;

        case 'c':   cout<<endl<<"Please choose Value Meal for Chicken"<<endl
                        <<"<F>ried Chicken"<<endl
                        <<"<G>rilled Chicken"<<endl
                        <<"<C>hef's Chicken"<<endl
                        <<"Chicken Meal -> ";
                        cin>>vmtype;

                    switch(vmtype)
                    {
                        case 'f':   vmname = "Fried Chicken";
                                    price = 4.00;
                                    break;
                        case 'g':   vmname = "Grilled Chicken";
                                    price = 5.00;
                                    break;
                        case 'c':   vmname = "Chef's Chicken";
                                    price = 6.00;
                                    break;
                        default:    cout<<"Invalid input, please come again!"<<endl;
                                     return 0;
                                    break;
                    }
                    break;
                   
        case 'p':   cout<<endl<<"Please choose Value Meal for Pasta"<<endl
                        <<"<S>paghetti de Italiano"<<endl
                        <<"<L>asagna Supreme"<<endl
                        <<"<M>acaroni Special"<<endl
                        <<"Pasta Meal -> ";
                        cin>>vmtype;

                        switch(vmtype)
                        {
                            case 's':   vmname = "Spaghetti de Italiano";
                                        price = 3.00;
                                        break;
                            case 'l':   vmname = "Lasagna Supreme";
                                        price = 4.00;
                                        break;
                            case 'm':   vmname = "Macaroni Special";
                                        price = 3.50;
                                        break;
                            default:    cout<<"Invalid input, please come again!"<<endl;
                                         return 0;
                                        break;
                        }
                    break;
                   
        default:    cout<<"Invalid Input, Please come again!"<<endl;
                     return 0;
                    break;
    }

    cout<<endl<<"No. of orders: ";
    cin>>order;

    tprice = price * order;

    cout<<endl<<"-------Summary of orders-------"<<endl<<endl
        <<"Value Meal -> "<<vmname<<endl
        <<"No. of orders -> "<<order<<endl
        <<"Price -> $"<<price<<endl
        <<"Total amount to pay -> is $"<<tprice<<endl
        <<"Enter Customer Cash -> $";
        cin>>cash;
    change = cash - tprice;
    cout<<"Customer Change -> $"<<change<<endl<<endl;
     return 0;
}

1 comment: