In: Computer Science
Part 3
Write a C++ program that prompts the user for an Account Number(a whole number). It will then prompt the user for the initial account balance (a double). The user will then enter either w, d, or z to indicate the desire to withdraw an amount from the bank, deposit an amount or end the transactions for that account((accept either uppercase or lowercase). You must use a nested if statements instead of a Switch construct.
Test Run:
Enter the account number: 1
Enter the initial balance: 5000
SAVINGS ACCOUNT TRANSACTION
(W)ithdrawal
(D)eposit
(Z) to end account transaction
Enter first letter of transaction type (W, D or Z): w
Amount: $1000
Balance for this account is now: $4000.00
SAVINGS ACCOUNT TRANSACTION
(W)ithdrawal
(D)eposit
(Z) to end account transaction
Enter first letter of transaction type (W, D or Z): d
Amount: $500
Balance for this account is now: $4500.00
SAVINGS ACCOUNT TRANSACTION
(W)ithdrawal
(D)eposit
(Z) to end account transaction
Enter first letter of transaction type (W, D or Z): z
No more changes.
Balance for this account is now: $4500.00
Press any key to continue . . .
Code
#include<iostream>
using namespace std;
int main()
{
int an; //account number
cout << "Enter the account number: ";
cin>>an;
double ib;
double bal=0;
cout<<"Enter the initial balance: ";
cin>>ib;
bal=bal+ib;
double amount;
char ch;
while(1)
{
cout<<"SAVINGS ACCOUNT TRANSACTION";
cout<<"\n(W)ithdrawal\n(D)eposit\n(Z) to end account transaction\nEnter first letter of transaction type (W, D or Z):";
cin>>ch;
if(ch=='z' || ch=='Z') //if z entered print messages and exit
{
cout<<"No more changes.\n";
printf("Balance for this account is now: %.2f",bal);
cout<<"\nPress any key to continue . . .";
break;
}
cout<<"Amount: $";
cin>>amount;
if(ch=='w' || ch=='W')
{
if(amount<=bal)
{
bal=bal-amount;
}
else // low balance
cout<<"balance is less than withdrawal amount";
}
if(ch=='d' || ch=='D')
{
bal=bal+amount;
}
printf("Balance for this account is now: $%.2lf",bal);
cout<<"\n";
}
return 0;
}
Terminal Work