1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
//bring in our libraries
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
#include <ctime>
#include <iomanip>
using namespace std;
//prototypes
void deposit(double* ptrBalance);
void withdrawal(double* ptrBalance, float dailyLimit);
void withdrawal(double* ptrBalance, float dailyLimit, float amount);
/// Entry point to the application
int main()
{
///Make a deposit
void deposit(double* ptrBalance);
{
//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" << endl;
deposit = -1; //set deposit to a "bad" number
continue; //restart the loop
}
else if (deposit < 0.0f) //check for negative number
cout << "\nError. Invalid deposit amount. \n" << 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) << "\nCurrent ptrBalance: $" << *ptrBalance << endl; //notice the asterisk
///Make a withdrawal
void withdrawal(double* ptrBalance, float dailyLimit);
{
//get the withdrawal (you should validate this input)
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, 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 << "\nError. Amount exceeds daily limit." << endl;
}
else if (amount > *ptrBalance) //notice the asterisk to dereferencethe pointer
{
cout << "\nError. Insufficent funds." << endl;
}
else
{
*ptrBalance -= amount; //same as: *ptrBalance = *ptrBalance - amount;
cout << "\nHere is your cash: $" << amount << endl;
}
cout << fixed << setprecision(2) << "\nCurrent Balance: $" << *ptrBalance << endl;
}
// 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 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();
}
else
{
//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;
// 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
cout << "Menu\n" << 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 << "\nEnter your choice: ";
cin >> choice;
//run code based on the user's choice
switch (choice)
{
case 1:
deposit(ptrBalance); //passing a pointer so only 4 bytes have to go across the system bus!
break;
case 2:
withdrawal(ptrBalance, DAILY_LIMIT);
break;
case 3:
//show the balance
cout << fixed << setprecision(2) << "\nCurrent balance: $" << balance << endl;
break;
case 4:
//get a quick $40
withdrawal(ptrBalance, DAILY_LIMIT, 40.0f);
break;
case 5:
cout << "\nGoodbye" << endl;
default:
cout << "\nError. Please select from the menu." << endl;
break;
}
//pause
cout << "\nPress any key to continue...";
_getch();
} while (choice != EXIT_VALUE);
// now that the app is over, write the new balance to file
ofstream oFile(FILENAME.c_str());
oFile << balance << endl;
oFile.close();
return 0;
}
|