//setprecision()
using namespace std;
// 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
///Entry point to the application
int main()
{
// create constant values -- cannot be changed
const int EXIT_VALUE = 5;
const float DAILY_LIMIT = 400.0F;
const string FILENAME = "Account.txt";
//create balance variable
double balance = 0.0;
// look for the starting balance; otherwise generate a ramdom starting balance
ifstream iFile(FILENAME.c_str());
if (iFile.is_open())
{
//did the file open? if so, read the balance
iFile >> balance;
iFile.close();
}
else
{
// if the file did not open or doesnot 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=""><>
//let's create a pointer and set it to the balance variable location
double* ptrBalance = &balance; // & means " address of"
//pause before we clear the screen
cout < "\npress="" 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\n"=""><>
cout < "1)="" deposit"=""><>
cout < "2)="" withdrawal"=""><>
cout < "3)="" check="" balance"=""><>
cout < "4)="" quick="" $40"=""><>
cout < "5)="" exit"=""><>
// get user input
cout < "\nenter="" 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!
cout < "making="" a="" withdrawal..."=""><>
break;
case 3:
// show the balance
cout < fixed="">< setprecision(2)="">< "\ncurrent="" balance:="" $"=""><>
break;
case 4:
// get a quick $40
withdrawal(ptrBalance, DAILY_LIMIT, 40.0f);
break;
case 5:
cout < "\ngoodbye"=""><>
break;
default:
cout < "\nerror.="" please="" select="" from="" the="" menu."=""><>
break;
}
// pause
cout < "\npress="" any="" key="" to="">
_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=""><>
oFile.close();
return 0;
// end of the main
///Make a deposit
void deposit(double* balance);
{
// get deposit and validate it
float deposit = 0.0f;
do
{
cout < "\nenter="" 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, '\n'); // clears keyboard buffer
cout < "\nerror.="" please="" use="" numbers="" only.\n"=""><>
deposit = -1; //set deposit to a "bad" number
continue; //restart the loop
}
if (deposit < 0.0f)="" check="" for="" negative="">
cout < "\nerror.="" invalid="" deposit="" amount.\n"=""><>
} while (deposit <>
// how do we get thedouble value located at the pointe?
//Derefence it using an asterisk!
balance += deposit;
cout < fixed="">< setprecision(2)="">< "\ncurrent="" prt="" balance:="" $"="">< balance="">< endl;="" notice="" the="">
}
/// Make a withdrawal
void withdrawal(double* ptrBalance, float dailyLimit);
{
// get the withdrawal
float amount = 0.0f;
cout < "\nenter="" withdrawal="" amount:="">
cin >> amount;
// call the overloaded method version that takes
// the balance, dailyLimit, and withdrawal amount
withdrawal(ptrBalance, DAILY_LIMIT, 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 > DAILY_LIMIT)
{
cout < "\nerror.="" amount="" exceeds="" daily="" limit."=""><>
}
else if (amount > * ptrBalance) //notice the asterisk to derefence the pointer!
{
cout < "\nerror.="" insufficient="" funds."=""><>
}
else
{
*ptrBalance -= amount; //same as:*ptrBalance - amount;
cout < "\nhere="" is="" your="" cash:="" $"="">< amount=""><>
}
cout < fixed="">< setprecision(2)="">< "\ncurrent="" balance:="" $"="">< *ptrbalance=""><>
}