Using what you know about inputs and outputs, code a simple ATM interaction. You
ID: 3796364 • Letter: U
Question
Using what you know about inputs and outputs, code a simple ATM interaction. Your ATM will start with only $500 to give out, and YOUR balance will be 1000. The amounts the ATM can dispense are $40, $80, $200 and $400. After you've received your disbursement, the console will show the amount you have left in your account, as well as how much the ATM has to dispense (we live in a small, fictional town - no one will use this information for nefarious purposes).
You may choose to use characters to show the interface, or just list the options for the user at the console.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Choose One
| $40 |$80
| $200 |$400
EXIT
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int option = 0;
int balance = 1000;
int withdrawal = 0;
cout<<" Initial Balance = "<<balance;
while(balance >0 && withdrawal <= 500) // withraw money until balance >0 and withdrawal <= 500
{
//Menu
cout<<" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
cout<<" Choose One ";
cout<<" 1. $40 ";
cout<<" 2. $80 ";
cout<<" 3. $200 ";
cout<<" 4. $400 ";
cout<<" 5. EXIT ";
cout<<" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
cout<<" Choose option";
cin>>option;
switch(option)
{
case 1: withdrawal = withdrawal + 40;
if(withdrawal > 500)
{
cout<<" Withdrawal cannot be greater than 500";
exit(0);
}
else
{
balance = balance - 40;
cout<<" Balance = "<<balance;
}
break;
case 2:
withdrawal = withdrawal + 80;
if(withdrawal > 500)
{
cout<<" Withdrawal cannot be greater than 500";
exit(0);
}
else
{
balance = balance - 80;
cout<<" Balance = "<<balance;
}
break;
case 3:
withdrawal = withdrawal + 200;
if(withdrawal > 500)
{
cout<<" Withdrawal cannot be greater than 500";
exit(0);
}
else
{
balance = balance - 200;
cout<<" Balance = "<<balance;
}
break;
case 4:
withdrawal = withdrawal + 400;
if(withdrawal > 500)
{
cout<<" Withdrawal cannot be greater than 500";
exit(0);
}
else
{
balance = balance - 400;
cout<<" Balance = "<<balance;
}
break;
case 5: exit(0);
default: cout<<" Invalid option";
break;
}
}
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.