In: Computer Science
I have given an example of step by step process to write menu program for a calculator .
Step 1:
First declare all the variable to be used in the program.
Step 2:
Then display all the the choice to the user for example:
cout<<"Press 1 to add";
cout<<"Press 2 to subtract";
cout<<"Press 3 to multiply"
Step 3:
The user will input their choice
cin>>choice
Step 4:
Then the switch case statement will be written:
switch(case) // the input of the user will trigger switch case and the case will be triggered according to users choice
{ and one can take any number or alphabet as input from user in order to trigger a particular case
case 1: in this example I have given case 1 one can also write case A or case a to trigger case accordingly
cout<<"Enter numbers"
cin>>a
cin>>b
sum=a+b;
cout<<sum;
break; // After every case break statemnt will break away from the switch case statement
case 2:
cout<<"Enter numbers"
cin>>a
cin>>b
sub=a-b;
cout<<sub;
break;
case 3:
cout<<"Enter numbers"
cin>>a
cin>>b
mul=a*b;
cout<<mul;
break;
default: // if the user input anything which is not in the case then this message will be printed
cout<<"Wrong input";
}
Step 5:
The program will end