Answer to Question #89783 in C++ for matimu

Question #89783
write a c++ program that acts as an ATM machine.
1
Expert's answer
2019-05-16T07:16:23-0400
#include <iostream>

const int AMOUNT = 1000;
const int PIN = 1234;
void show_menu() {
	std::cout << "\nPlease select transaction:\n";
	std::cout << "1. Balance\n2. Cash Withdraw\n3. Cancel\n";
}
void withdraw_menu() {
	std::cout << "\nSelect amount:\n";
	std::cout << "1. $20\n2. $50\n3. $100\n4. $200\n";
	std::cout << "5. Enter another amount\n";
}

int main()
{
	int user_pin, menu_choice, amount_choice;
	int user_amount = AMOUNT;
	int withdraw_amount;

	std::cout << "WELCOME\nPlease enter PIN: ";
	
	for (int i = 0; i < 3; i++) {
		std::cin >> user_pin;
		if (user_pin == PIN) break;
		std::cout << "\nINCORECT PIN\n";
		if (i < 2) std::cout << "Try again (" << 2 - i << " attempts left)\n";
		else {
			std::cout << "Terminated";
			exit(0);
		}
	}

	while (true) {
		show_menu();

		std::cin >> menu_choice;
		if (menu_choice == 1) {
			std::cout << "\nAvalable balance: $" << user_amount << "\n";
		}
		else if (menu_choice == 2) {
			withdraw_menu();
			std::cin >> amount_choice;
			if (amount_choice == 1) withdraw_amount = 20;
			else if (amount_choice == 2) withdraw_amount = 50;
			else if (amount_choice == 3) withdraw_amount = 100;
			else if (amount_choice == 4) withdraw_amount = 200;
			else if (amount_choice == 5) {
				std::cout << "Enter amount: ";
				std::cin >> withdraw_amount;
			}
			if (withdraw_amount > user_amount) {
				std::cout << "WARRING!\nTransaction cannot be processed\n";
			}
			else {
				std::cout << "\nTransaction completed\n\n";
				user_amount -= withdraw_amount;
			}
		}
		else if (menu_choice == 3) break;
	}

	std::cout << "\nThank you!\n";

	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS