Question: ATM Application visual studio c++ This is what i have so far: // bring in your libraries #include #include #include #include // read/write to files
ATM Application visual studio c++
This is what i have so far:
// bring in your libraries
#include
#include
#include
#include
#include
#include
using namespace std;
/// Entry point to the application
// create constant values -- cannot be changed
const int EXIT_VALUE = 5;
const float DAILY_LIMIT = 400.0f;
const string FILENAME = "Accout.txt";
// create balance variable
double balance = 0.0;
// prototypes
void deposit(double* ptrBalance);
void withdrawal(double* ptrBalance, float dailyLimit); // overloaded method - this version does not take withdrawal amount
void withdrawal(double* ptrBalance, float dailyLimit, float amount); // overloaded method that takes withdrawal amount
int main()
{
/// Make a deposit
void deposit(double* ptrBalance)
{
// get deposit and validate it
float deposit = 0.0f;
do
{
cout << " Enter deposit amount: ";
cin >> deposit;
if (cin.fail()) // did they give us a character instead of a number?
{
cin.clear(); // clears fail state
cin.ignore(INT16_MAX, ' '); // clears keyboard buffer
cout << " Error. Please use numbers only. " << endl;
deposit = -1; // set deposit to a "bad" number
continue; // restart the loop
}
else if (deposit < 0.0f) // check for negative number
cout << " Error. Invalid deposit amout. " << endl;
} while (deposit < 0.0f)
// how do we get the double value located at the pointer?
// Dereference it using an asterisk!
*ptrBalance += deposit; // same as: ptrBalance = *ptrBalance + deposit;
cout << fixed << setprecision(2) << " Current ptrBalance: $" << *ptrBalance << endl;
// notice the astersk
}
/// Make a withdrawal
void withdrawal(double* ptrBalance, float dailyLimit)
{
// get the withdrawal (you should validate this input)
float amout = 0.0f;
cout << " Enter withdrawal amount: ";
cin >> amount;
// call the overloaded method version that takes
// the balance, dailyLimit, and withdrawal amount
withdrawal(ptrBalance, dailyLimit, amount);
}
/// Make a withdrawal - this overload accepts balance, dailyLimit, and withdrawal amount
void withdrawal(double* ptrBalance, float dailyLimit, float amount)
{
// take away money from the account and show the balance
if (amount > dailyLimit)
{
cout << " Error. Amount exceeds daily limit." << endl;
}
else if (amount > * ptrBalance) // notice the asterisk to dereference the pointer!
{
cout << " Error. Insufficient funds." << endl;
}
else
{
*ptrBalance -= amount; // same as: *ptrBalance = *ptrbalance - amount;
cout << " Here is your cash: $" << amount << endl;
}
cout << fixed << setprecision(2) << " Current Balance: $" << *ptrBalance << endl;
}
// look for the starting balance; otherwise generate a random starting balance
ifstream iFile(FILENAME.c_str());
if (iFile.is_open())
{
// did the file open? if so, read the balance
iFile >> balance;
iFile.close();
}
{
// if the file did not open or does not exist, create a
// random number for the starting balance
srand(time(0));
const int MIN =1000;
const int MAX =10000;
balance = rand() % (MAX - MIN + 1) + MIN;
}
cout << fixed << setprecision(2) << "Starting Balance: $" << balance << endl;
// lets create a pointer and set it to the balance variable location
double* ptrBalance = &balance;// & means "address of"
// pause before we clear the screen
cout << " Press any key to continue...";
_getch();
// create loop variable BEFORE the loop
short choice = 0;
// start the application loop
do
{
// show the menu
system("cls"); // clears the console screen -- for Mac, use system("clear")
cout << "Menu " << endl;
cout << "1) Deposit " << endl;
cout << "2) Withdrawal" << endl;
cout << "3) Check Balance" << endl;
cout << "4) Quick $40" << endl;
cout << "5) Exit" << endl;
// get user input
cout << " Enter your choice: ";
cin >> choice;
// run code based on the user's choice
switch (choice)
{
case 1:
deposit(ptrBalance); // passing a pointer so only four bytes have to go across the system bus!
break;
case 2:
withdrawal(ptrBalance, DAILY_LIMIT); // passing four byte pointer!
break;
case 3:
// show the balance
cout << fixed << setprecision(2) << " Current Balance: $" << balance << endl;
break;
case 4:
// get a quick $40
withdrawal(ptrBalance, DAILY_LIMIT, 40.0f);
break;
case 5:
cout << " Goodbye..." << endl;
break;
default:
cout << " Error. Please select from the menu." << endl;
break;
}
// pause
cout << " Press any key to continue...";
_getch();
}
while (choice != EXIT_VALUE);
// now that the application is over, write the new balance to the file
ofstream oFile(FILENAME.c_str());
oFile << balance << endl;
oFile.close();
return 0;
// pause
cout << " Press any key to continue...";
_getch();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
