Explore solved C++ do-while loop exercises, including a menu-driven calculator, algorithm analysis, dry runs, complete code, and sample output.
The task is to develop a menu-driven calculator using a do-while loop.
The program should repeatedly display a menu and allow the user to perform addition or subtraction until the user selects Exit.
Input:
1, 2, or 3).Processing:
1, add the two numbers.2, subtract the second number from the first.3, terminate the loop.Output:
Calculator closed. when the user exits.Programming concepts required:
do-while loopswitch statement1:
2:
3:
Calculator closed.3.Pseudocode
START
DO
DISPLAY calculator menu
INPUT choice
SWITCH choice
CASE 1:
INPUT num1, num2
sum = num1 + num2
DISPLAY sum
BREAK
CASE 2:
INPUT num1, num2
difference = num1 - num2
DISPLAY difference
BREAK
CASE 3:
DISPLAY "Calculator closed."
BREAK
DEFAULT:
DISPLAY "Invalid choice."
WHILE choice is not equal to 3
STOP
A dry run helps us understand how the program executes with the given sample input.
Given input:
1
10 5
2
10 5
3
| Step | Choice | Number 1 | Number 2 | Operation / Action | Result |
|---|---|---|---|---|---|
| 1 | 1 | 10 | 5 | Addition | 15 |
| 2 | 2 | 10 | 5 | Subtraction | 5 |
| 3 | 3 | — | — | Exit | Calculator closed. |
Understanding the loop:
10 + 5 = 15.10 - 5 = 5.3.#include <iostream>
using namespace std;
int main()
{
int choice;
float num1, num2;
do
{
cout << "\n===== Simple Calculator =====" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Sum = " << num1 + num2 << endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Difference = " << num1 - num2 << endl;
break;
case 3:
cout << "Calculator closed." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 3);
return 0;
}
Let’s understand the important parts of the program.
int choice;
float num1, num2;
choice stores the menu option entered by the user.num1 stores the first number.num2 stores the second number.float allows the calculator to work with decimal numbers.do
{
// Menu and calculator operations
} while (choice != 3);
A do-while loop executes its body at least once before checking the condition.
Here, choice != 3 means the loop continues as long as the user does not select Exit.
When the user enters 3, the condition becomes false, and the loop terminates.
cout << "\n===== Simple Calculator =====" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Exit" << endl;
These statements display the calculator menu each time the loop executes.
cout << "Enter your choice: ";
cin >> choice;
The program asks the user to select an option and stores it in the choice variable.
switch (choice)
The switch statement checks the selected menu option and executes the matching case.
case 1: Performs addition.case 2: Performs subtraction.case 3: Displays the closing message.default: Handles invalid choices.cin >> num1 >> num2;
cout << "Sum = " << num1 + num2 << endl;
The program takes two numbers and adds them using the + operator.
cin >> num1 >> num2;
cout << "Difference = " << num1 - num2 << endl;
The program subtracts the second number from the first using the - operator.
break;
The break statement terminates the current switch case. It does not terminate the do-while loop.
The loop continues until the user selects 3.
===== Simple Calculator =====
1. Addition
2. Subtraction
3. Exit
Enter your choice: 1
Enter two numbers: 10 5
Sum = 15
===== Simple Calculator =====
1. Addition
2. Subtraction
3. Exit
Enter your choice: 2
Enter two numbers: 10 5
Difference = 5
===== Simple Calculator =====
1. Addition
2. Subtraction
3. Exit
Enter your choice: 3
Calculator closed.
Now modify the existing calculator program to practise independently.
3. Multiplication.3, ask for two numbers.3 to 4.Calculator closed. when the user selects 4.4. Division.Challenge: Update the menu and program so that all four arithmetic operations work correctly, and the user can continue calculating until selecting Exit.
Key Learning Point: The do-while loop is useful for menu-driven programs because it displays the menu at least once and repeats it until the user chooses to exit.