C++ Online Compiler
Example: ATM Cash Withdrawal Simulation in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// ATM Cash Withdrawal Simulation #include <iostream> // Required for input/output operations #include <iomanip> // Required for setting precision for currency display using namespace std; int main() { // Step 1: Initialize account balance double accountBalance = 1000.00; // Starting balance of the account double withdrawalAmount; // Variable to store the user's requested amount // Step 2: Display welcome message and current balance cout << "Welcome to the ATM!" << endl; cout << fixed << setprecision(2); // Format output to two decimal places for currency cout << "Your current balance is: $" << accountBalance << endl; // Step 3: Prompt user for withdrawal amount cout << "Enter amount to withdraw: $"; cin >> withdrawalAmount; // Step 4: Validate the withdrawal amount and process transaction if (withdrawalAmount <= 0) { cout << "Invalid amount. Please enter a positive number." << endl; } else if (withdrawalAmount > accountBalance) { cout << "Insufficient funds. Your current balance is $" << accountBalance << "." << endl; } else { // Step 4a: Process successful withdrawal accountBalance -= withdrawalAmount; // Deduct the amount from the balance cout << "Transaction successful! Please take your cash." << endl; cout << "Your new balance is: $" << accountBalance << endl; } return 0; // Indicate successful program execution }
Output
Clear
ADVERTISEMENTS