Hotel Management using Switch Case in C++


Total price based on the drink and quantity selected using a switch statement. Here's a breakdown of the code:

  • int a,qty,rate,total; This line declares four variables: a to store the user's drink selection, qty to store the quantity entered by the user, rate to store the price of the selected drink, and total to store the total price.
  • cout<<"\nMENU CARD \nSelect your drink \n1.COFFEE \n2.TEA \n3.COLD COFFEE \n4.MILK SHAKE \n5.JUICE\n"; These lines display the menu of drinks to the user using the cout function.
  • cout<<"\nEnter the number :"; cin>>a; This line prompts the user to enter the number of the selected drink and reads the input into the a variable using the cin function.
  • switch(a) { ... } This is a switch statement that takes the value of the a variable and executes the corresponding case. If a is 1, it will execute the code block after case 1. If a is 2, it will execute the code block after case 2, and so on. If a doesn't match any of the cases, it will execute the code block after the default case.
  • cin>>qty; rate=5; total=qty*rate; cout<<"\nTotal amount :"<<total; break; These are examples of the code block that is executed when the a variable matches the corresponding case. For example, when a is 1, it will prompt the user to enter the quantity of coffee using the cout function, read the input into the qty variable using the cin function, set the price of coffee to 5 using the rate variable, calculate the total price by multiplying the quantity and price using the total variable, and output the total price using the cout function.
  • break; This keyword is used to exit the switch statement and continue executing the program after the switch statement.
  • cout<<"\nSorry Unavailable...s"<<a; This line is the code block that is executed when the a variable doesn't match any of the cases. It outputs "Sorry Unavailable" followed by the user's selection number using the cout function.
  • return 0; This line ends the program and returns 0 to the operating system to indicate that the program executed successfully.

Source Code

#include<iostream>
using namespace std;
int main()
{
  int a,qty,rate,total;
  cout<<"\nMENU CARD \nSelect your drink \n1.COFFEE \n2.TEA \n3.COLD COFFEE \n4.MILK SHAKE \n5.JUICE\n";
  cout<<"\nEnter the number :";
  cin>>a;
  switch(a)
  {
  case 1:
    cout<<"\nYou have selected Coffee.\n Enter the quantity :";
    cin>>qty;
    rate=5;
    total=qty*rate;
    cout<<"\nTotal amount :"<<total;
    break;
  case 2:
    cout<<"\nYou have selected Tea.\n Enter the quantity :";
    cin>>qty;
    rate=10;
    total=qty*rate;
    cout<<"\nTotal amount :"<<total;
    break;
  case 3:
    cout<<"\nYou have selected Cold coffee.\n Enter the quantity :";
    cin>>qty;
    rate=15;
    total=qty*rate;
    cout<<"\nTotal amount :"<<total;
    break;
  case 4:
    cout<<"\nYou have selected Milk shake.\n Enter the quantity :";
    cin>>qty;
    rate=20;
    total=qty*rate;
    cout<<"\nTotal amount :"<<total;
    break;
  case 5:
    cout<<"\nYou have selected Juice.\n Enter the quantity :";
    cin>>qty;
    rate=15;
    total=qty*rate;
    cout<<"\nTotal amount :"<<total;
    break;
  default:
    cout<<"\nSorry Unavailable...s"<<a;
    break;
  }
  return 0;
}
To download raw file Click Here

Output

MENU CARD 
Select your drink 
1.COFFEE 
2.TEA 
3.COLD COFFEE 
4.MILK SHAKE 
5.JUICE

Enter the number :4
You have selected Milk shake.
 Enter the quantity :2
 Total amount :40

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples