In: Computer Science
code :
#include <iostream>
#include <string>
using namespace std;
// function to earn money
int earn(int deposit, int balance);
// function to spend money
int spend(int withdrawl, int balance);
// function to track balance
void showLogs(string logs);
main(){
// storing account balance
int balance = 0;
// storing logs
string logs = "";
// flag is for running the program continuosly untill user manually exits.
// program runs for flag=1 and exists if flag=0
// logged is used to track user correct pin to avoid re-login
// when program starts again
int flag = 1, logged = 0;
while (flag == 1){
// doesn't log if user is already logged
if (logged == 0){
int pin;
cout << "Welcome!\nEnter pin: ";
cin >> pin;
// pin check
if(pin != 5678 && pin != 8765){
cout << "Wrong PIN\n";
continue;
}
logged = 1;
// prompt menu
cout << "\nMenu\n1. Earn Money\n2. Spend Money\n3. Show Logs\n4. Exit\n";
}
cout << "\nEnter your Option: ";
int option;
cin >> option;
switch (option){
case 1:
cout << "Enter amount to deposit: ";
// deposit stores amount user needs to earn
// bal stores the balance returned after executing the earn function
int deposit, bal;
cin >> deposit;
bal = earn(deposit, balance);
// error if bal is -1, so break without updating balance and logs
if(bal == -1)
break;
// if no error, update original balance and logs
balance = bal;
logs += "[+] Earned money ";
logs += to_string(deposit);
logs += "\n";
cout << "Succesfully earned money!\n";
break;
case 2:
cout << "Enter amount to spend: ";
// withdrawl stores amount user needs to spend
// bal2 stores the balance returned after executing
// the withdram function
int withdrawl, bal2;
cin >> withdrawl;
bal2 = spend(withdrawl, balance);
// error if bal is -1, so break without updating balance and logs
if(bal2 == -1)
break;
// if no error, update original balance and Logs
balance = bal2;
logs += "[-] Spent money ";
logs += to_string(withdrawl);
logs += "\n";
cout << "Succesfully spent money!\n";
break;
case 3:
// showing logs stored in a string
cout << logs;
// showing the current balance
cout << "[=] Current Balance " << balance << "\n";
break;
case 4:
// exiting the program
// flag made 0 to exit out of the loop to terminate the program
flag = 0;
// also make logged 0 to log out the user
logged = 0;
cout << "Exiting Program!\n";
// no other code in this loop below is executed on
// exit and control reaches to start of loop.
// this is done using continue
continue;
default:
// if no option exits, prompt `invalid option`
cout << "Invalid Option! Choose Other\n";
break;
}
}
}
int earn(int deposit, int balance){
// if deposit amount is negetive raise error by returning -1.
// return value -1 is handled as error in above program
if(deposit < 0){
cout << "Cannot deposit negetive amount\n";
return -1;
}
// update balance and return balance
balance = balance + deposit;
return balance;
}
int spend(int withdrawl, int balance){
// if withdrawl amount is negetive raise error by returning -1.
// return value -1 is handled as error in above program
if(withdrawl < 0){
cout << "Cannot widthdraw negetive amount\n";
return -1;
}
// if withdrawl is greater then current balance raise error by returning -1.
if(withdrawl > balance){
cout << "No sufficient amount to withdraw\n";
return -1;
}
// update balance and return balance
balance = balance - withdrawl;
return balance;
}
Output:
Have a nice day