Group
Advanced Conditional Structures in C++
Objective
1. Start with a default account balance (e.g., $1000).
2. Show a menu with the following options:
- 1. Check Balance
- 2. Withdraw
- 3. Deposit
- 4. Exit
3. Based on the user's choice, perform the corresponding action.
4. For withdrawals, ensure that the amount does not exceed the available balance.
5. Repeat the menu until the user chooses to exit.
Write a program that simulates an ATM with multiple options such as withdrawal, balance inquiry, deposit, and exit.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include input/output stream library
using namespace std;
int main() {
int choice; // Variable to store user's menu selection
double balance = 1000.0; // Initial balance set to $1000
double amount; // Variable for deposit or withdrawal amount
// Use a loop to keep showing the menu until user exits
do {
// Display the ATM menu
cout << "\nATM Menu:\n";
cout << "1. Check Balance\n";
cout << "2. Withdraw\n";
cout << "3. Deposit\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
// Display current balance
cout << "Your current balance is: $" << balance << endl;
break;
case 2:
// Prompt user for withdrawal amount
cout << "Enter amount to withdraw: ";
cin >> amount;
// Check if sufficient balance is available
if (amount <= balance) {
balance -= amount;
cout << "Withdrawal successful. New balance: $" << balance << endl;
} else {
cout << "Insufficient balance." << endl;
}
break;
case 3:
// Prompt user for deposit amount
cout << "Enter amount to deposit: ";
cin >> amount;
// Add deposit to balance
balance += amount;
cout << "Deposit successful. New balance: $" << balance << endl;
break;
case 4:
// Exit message
cout << "Thank you for using the ATM. Goodbye!" << endl;
break;
default:
// Handle invalid option
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4); // Loop ends when user selects exit
return 0; // End of program
}
Output
ATM Menu:
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Enter your choice: 1
Your current balance is: $1000
ATM Menu:
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Enter your choice: 2
Enter amount to withdraw: 300
Withdrawal successful. New balance: $700
ATM Menu:
1. Check Balance
2. Withdraw
3. Deposit
4. Exit
Enter your choice: 4
Thank you for using the ATM. Goodbye!