Function to Withdraw amount from an array of balance - c++

I'm to write a function to withdraw money from array containing initial balance.
If the balance is insufficient, then a warning message along with user's balance is displayed.
The user is prompted to enter the new amount for a second time, if the amount is OK withdraw operation is completed and balance is updated and menu is displayed.
If balance is still insufficient, withdraw operation is terminated and menu is displayed.
Below is the code which works if I try to enter amount which is lower than the balance for the first time. But does not update the balance well if I enter the valid amount on the second try.
double withdrawAmount(int pin, double balance[]){
double amount;
double newBalance = balance[pin];
int withdrawTrial = 0;
do{
system("cls");
cout << "-----------------------------------------" << endl;
cout << "\tWELCOME TO EDON ATM" << endl;
cout << "-----------------------------------------" << endl;
cout << endl;
cout << "Please enter amount to be withdrawn: $";
cin >> amount;
if (balance[pin] < amount){
cout << endl;
cout << "Insufficient Balance!" << endl;
cout << "Your balance is: $" << balance[pin] << endl;
cout << endl;
while (withdrawTrial < 1){
withdrawTrial++;
cout << "Please enter amount to be withdrawn: $";
cin >> amount;
}
if (withdrawTrial == 1){
menuBoard();
return newBalance;
}
}
balance[pin] -= amount;
newBalance = balance[pin];
} while (amount < 0);
return newBalance;
}

You are not following the instructions correctly. You are not validating the user's input on the second prompt. Get rid of the while (withdrawTrial < 1) loop completely. Prompt once and validate. If the amount is greater than the available balance then prompt again and re-validate. If the amount is still greater than the available balance then exit. No loops are needed (unless you want to validate the user is actually entering a floating-point number and nothing else).
Also, you should not be calling menuBoard() inside of withdrawAmount() itself, you should be calling it after withdrawAmount() exits.
double inputDollars(const char *prompt){
double value;
do {
cout << prompt << ": $";
if (cin >> value)
break;
cout << endl;
cout << "Invalid Input!" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
while (true);
return value;
}
double withdrawAmount(int pin, double balance[]){
double amount;
system("cls");
cout << "-----------------------------------------" << endl;
cout << "\tWELCOME TO EDON ATM" << endl;
cout << "-----------------------------------------" << endl;
cout << endl;
amount = inputDollars("Please enter amount to be withdrawn");
if (balance[pin] < amount){
cout << endl;
cout << "Insufficient Balance!" << endl;
cout << "Your balance is: $" << balance[pin] << endl;
cout << endl;
amount = inputDollars("Please enter amount to be withdrawn");
if (balance[pin] < amount){
cout << endl;
cout << "Insufficient Balance!" << endl;
return balance[pin];
}
}
balance[pin] -= amount;
return balance[pin];
}
...
double balances[N];
int pin;
...
withdrawAmount(pin, balances);
menuBoard();

Related

I am having problems with rejecting negatives within my function

I was doing a school assignment involving functions, it wasn't too hard until I learned I needed to reject negative numbers as variables. It doesn't reject the numbers and instead just skips the next variable and then will loop to a seemingly random variable random variables in the code when the function completes.
void InPatient()
{
int DaysIn = 0;
double DailyRate{};
double MedicationCharges{};
double ServiceCharges{};
cout << "Please enter number of days patient stayed (Rounded up):" << endl;
cin >> DaysIn;
//I tried to use while statements to solve this problem and it hasn't worked
while (DaysIn != 50000000000)
{
if (DaysIn >= 0)
{
cout << "Please enter hospital's daily rate as a decimal (Ex: .35, .265):" << endl;
cin >> DailyRate;
}
else if (DaysIn < 0)
{
cout << "That is not a valid number in the system" << endl;
}
if (DailyRate >= 0)
{
cout << "Please enter medicine charges:" << endl;
cin >> MedicationCharges;
}
else if (DailyRate < 0)
{
cout << "That is not a valid number in the system" << endl;
}
//I tried these else if statements to reject negative numbers and loop back but it just says "It doesn't work" and continues on.
if (MedicationCharges >= 0)
{
cout << "Please enter service charges (for lab testing or whatnot):" << endl;
cin >> ServiceCharges;
}
else if (MedicationCharges < 0)
{
cout << "That is not a valid number in the system" << endl;
}
if (ServiceCharges >= 0)
{
double DaysInFee = DaysIn / DailyRate;
double HospitalBill = DaysInFee + MedicationCharges + ServiceCharges;
cout << "Patient's Stay Fee: $" << DaysInFee << "\n";
cout << "Medication Charges: $" << MedicationCharges << "\n";
cout << "Service Charges: $" << ServiceCharges << "\n";
cout << "Patient's total is $" << std::setprecision(2) << std::fixed << HospitalBill << " " << "today." << endl;
}
else if (ServiceCharges < 0)
{
cout << "That is not a valid number in the system" << endl;
}
}
}
void OutPatient()
{
double MedicationCharges = 0;
double ServiceCharges{};`
//I've done something different down here, but it does the exact same thing
while (MedicationCharges != 50000000000)
{
cout << "Please enter medicine charges:" << endl;
cin >> MedicationCharges;
cout << "Please enter service charges (for lab testing or whatnot):" << endl;
cin >> ServiceCharges;
double HospitalBill = MedicationCharges + ServiceCharges;
cout << "Medication Charges: $" << MedicationCharges << "\n";
cout << "Service Charges: $" << ServiceCharges << "\n";
cout << "Patient's total is $" << std::setprecision(2) << std::fixed << HospitalBill << " " << "today." << endl;
if (MedicationCharges < 0)
{
cout << "That is not a valid number in the system" << endl;
}
//These just say "These don't work" but let's the code use them anyways.
else if (ServiceCharges < 0)
{
cout << "That is not a valid number in the system" << endl;
}
}
}
As stated in the code, I have used while and if statements to reject negatives, but it doesn't reject them.
Add a continue in your else if blocks to restart the while-loop after an incorrect input
Alternatively, add a break or return statement to leave the loop or function. It depends what you want to do in the error case.

most of the code doesn't show up only the welcome message and "enter pin"?

Assignment:
When you enter an incorrect number of digits( 3 or 5 digits pin number) a
message should display “You have entered the incorrect pin number!!...you
must enter a four digits pin number.”
By showing the message that was display previously the program should
allow you to re-enter the correct number of digits.
The number of attempts should be three times.
When you have entered the correct number of digits a message should display
“Your pin has been accepted!!”
Each time you enter any amount of pin number it must show in asterisk.
Code:
#include <conio.h>
#include <iostream>
//#include <cstdlib.h>
#include <cmath>
using namespace std;
int main() {
string pass = "";
int attempts = 3;
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "< Welcome to The Bank >" << endl;
cout << "< >" << endl;
cout << "< Please Enter Pin Below >" << endl;
cout << " ^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "\nEnter Pin Number: " << endl;
cin >> pass;
// attempts= getch();
while (attempts <= 3) {
cout << "*";
getch();
attempts++; // take this out and it display to infinity
// }
if (pass == "1718") {
cout << " lOGIN IN..." << endl << endl;
attempts = -1;
}
else {
cout << "\nWRONT PIN-TRY AGAIN: " << endl << endl;
attempts--;
cout << " REMAINING ATTEMPTS: " << attempts << endl << endl;
}
if (attempts == 0) {
cout << "Exceed the Pin attempts. Try Later. " << endl << endl;
}
if (attempts == -1) {
cout << "********************************" << endl;
cout << "* Welcome to Magali's Bank *\n";
cout << "* Select Option: *\n";
cout << "* 1. Check Balance *\n";
cout << "* 2. Withdraw *\n";
cout << "* 3. Deposit *\n";
cout << "* 4. Exit *\n";
cout << "********************************\n";
int balance = 500;
float withdraw;
float deposit;
int user;
cout << "Enter Number: ";
cin >> user;
while (user != 4) {
switch (user) {
case 1:
cout << " Your balance is: " << balance << endl;
break;
case 2:
cout << "Enter the amount you want withdraw: ";
cin >> withdraw;
balance = balance - withdraw;
break;
case 3:
cout << "Enter the amount you want to deposit: ";
cin >> deposit;
balance = balance + deposit;
break;
default:
cout << " Need to type 1 for Balance, 2 to Withdraw, 3 to Deposit "
"and 4 to Exit. ";
}
cout << "Enter Number: ";
cin >> user;
}
cout << "\nTHANKS FOR USING THE SYSTEM!\n";
}
}
return 0;
}
Your code has lots of inefficiencies but as you requested. I'm only debugging only a part of it where you need to take input and display error.
string pass="";
int attempts=3;
const string pin = "1718";
//DO whatever you want
//while loop to take input
while (attempts >0)
{
cout << "\nEnter Pin Number: " << endl;
cin >> pass;
for(auto i : pass)
cout<<"*";
if(pass == pin){
attempts = 3;
cout<<"Your Pin has been accepted.\n";
break;}
else{
cout <<"\nWRONT PIN-TRY AGAIN: " << endl << endl;
attempts--;
cout << " REMAINING ATTEMPTS: " << attempts << endl << endl;
}
}
if (attempts == 0)
{
cout << "Exceed the Pin attempts. Try Later. "<< endl << endl;
cout<<"Your account has been locked for a day .\n";
exit(0);
}
Comment out if you didn't understand any part.

Cases and Switch Issue

I am writing a program that simulates an ATM. So it tracks account balances, withdrawals and deposits in a very basic matter.
Everything works well during the first iteration, but if I go to make two or more deposits or withdrawals, the account balances default back to the original amount.
Here is an example of what is currently happening: I have $1,000 in my account initially. I make a deposit of $50. It prints out that I now have $1,050 in my account and asks if I would like to perform any other actions. (This is all good). If I select that I want to make another deposit of $100, it says my new account balance is $1,100 instead of $1,150. It does not store my latest account balance when I perform new withdrawals or deposits.
The second (less important) issue is that each time a withdrawal is made, there is a $2.50 fee for each withdrawal that also gets subtracted from my account balance.
I have not learned loops yet, only Cases, If statements and If Else Statements.
Is it possible to do what I want to do? Below is my code. Thank you in advance! This is my first time posting, so if I have pasted my code wrong, I apologize.
#include <iostream>
#include <string>
using namespace std; // opens library for "cout"
int main()
{
float test;
int logout;
string name;
float balance;
float fee;
int choice;
float withdraw;
float deposit;
float bonus;
bonus = 2.50;
balance = 1572.36;
fee = 12.50;
char answer;
cout << "Hello, thank you for banking with Pallet Town Bank.\n";
cout << "Please enter your name. ";
cin >> name;
cout << "Hello " << name << ". Your current balance is $" << balance << ".\n";
cout << "There will be a a service fee of $12.50 subtracted from your "
"account.\n";
cout << "Your updated balance will be $" << balance - fee << " \n";
cout << "What would you like to do today?\n";
do
{
cout << "\n1 - Current Balance\n2 - Withdraw\n3 - deposit\n4 - Log "
"Out\nOption: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "\nCurrent Balance is " << balance - fee - withdraw + deposit
<< " \n";
cout << "Would you like to take any other actions today?\n";
break;
case 2:
cout << "\nWithdraw - How much would you like to withdraw? $";
cin >> withdraw;
cout << "Your new balance after withdrawing $" << withdraw << " will be $"
<< balance - fee - withdraw + deposit << "\n";
cout << "Would you like to take any other actions today?\n";
break;
case 3:
cout << "\nDeposit - How much would you like to deposit? $";
cin >> deposit;
test = balance - fee - withdraw + deposit;
cout << "Your new balance after depositing $" << deposit << " will be $"
<< test << endl; //<<balance - fee - withdraw + deposit<<"\n";
cout << "Would you like to take any other actions today? Y or N \n";
cin >> answer;
cout << answer;
if (answer == 'y' || 'Y')
{
test = balance - fee - withdraw + deposit + deposit;
cout << "Your new balance after depositing $" << deposit << " will be $"
<< test << endl;
}
// cout <<"Your new balance after depositing $"<<deposit<<" will be $"
// <<test<< endl; //<<balance - fee - withdraw + deposit<<"\n";
// cout <<"Would you like to take any other actions today?\n";
break;
case 4:
cout << "\nLog Out - Thank you for banking with Pallet Town Bank. Have "
"a great day!";
}
} while (choice != 4);
}
The main issue, as Alan Birtles points out, is that you never update the balance value; you just store the temporary calculations in the test variable.
Here's how you could change your code. I tried interpreting the expected behaviour of the program as best I could from the text:
#include <iostream>
#include <string>
using namespace std;
int main()
{
const double fee = 12.50;
double balance = 1572.36;
cout << "Hello, thank you for banking with Pallet Town Bank.\n";
cout << "Please enter your name. ";
string name;
cin >> name;
cout << "Hello " << name << ". Your current balance is $" << balance << ".\n";
cout << "There will be a a service fee of $12.50 subtracted from your "
"account.\n";
cout << "Your updated balance will be $" << (balance -= fee) << " \n";
cout << "What would you like to do today?\n\n";
while (true)
{
cout << "1 - Current Balance" << '\n'
<< "2 - Withdraw" << '\n'
<< "3 - deposit" << '\n'
<< "4 - Log Out" << '\n'
<< "Option: ";
int choice;
cin >> choice;
cout << endl;
if (choice == 4) break;
switch (choice)
{
case 1:
cout << "Current Balance is " << balance << '\n';
break;
case 2:
cout << "Withdraw - How much would you like to withdraw? $";
double withdraw;
cin >> withdraw;
cout << "Your new balance after withdrawing $" << withdraw << " will be $"
<< (balance -= withdraw) << '\n';
break;
case 3:
cout << "Deposit - How much would you like to deposit? $";
double deposit;
cin >> deposit;
cout << "Your new balance after depositing $" << deposit << " will be $"
<< (balance += deposit) << '\n';
break;
}
cout << "Would you like to take any other actions today? ";
char answer;
cin >> answer;
cout << endl;
if (toupper(answer) == 'N') break;
}
cout << "Log Out - Thank you for banking with Pallet Town Bank. Have a great day!" << endl;
}
Live demo
Changes
First of all, every time a modification is made (withdrawal/deposit), we need to actually update the balance. We can do so by using the += or -= operators (called "compound assignment" operators): when writing balance += x, we're adding x to balance, and when writing balance -= x we're subtracting. These expressions are equivalent to balance = balance + x and balance = balance - x, respectively.
I moved the "Would you like to [...]" part outside the switch statement, to avoid repeating it in each case.
As pointed out in the comments, answer == 'Y' || 'y' isn't the same as answer == 'Y' || answer == 'y'. I changed that to toupper(answer) == 'Y'.
I moved the logout handling outside the while loop, so that whenever the loop terminates, the logout message is always shown. That allows us to remove case 4 from the switch statement, by checking at the beginning if choice == 4 and then breaking out of the loop accordingly. This also implies the loop becomes a while (true) loop. Probably there is a more elegant way.
Even better
If you're confortable with functions, I would suggest refactoring the code, isolating each operation individually:
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
const int FEE = 1250; // fee in cents
//---- Utilities ----//
string moneyString(int cents) {
ostringstream oss;
oss << cents/100 << '.' << cents % 100;
return oss.str();
}
int toCents(double money) {
return int(round(money*100));
}
int getMoney() {
double money;
cin >> money;
return toCents(money);
}
//---- User input ----//
// Available choices
enum Choices {
BALANCE = 1,
WITHDRAW = 2,
DEPOSIT = 3,
LOGOUT = 4
};
short int getChoice() {
short int choice = 0;
while (choice < 1 or choice > 4) {
cout << "1 - Current Balance" << '\n'
<< "2 - Withdraw" << '\n'
<< "3 - deposit" << '\n'
<< "4 - Log Out" << '\n'
<< "Option: ";
string input;
cin >> input;
choice = atoi(input.c_str());
cout << endl;
}
return choice;
}
bool userWantsMoreActions() {
cout << "Would you like to take any other actions today? ";
char answer;
cin >> answer;
cout << endl;
return toupper(answer) == 'Y';
}
//---- Actions ----//
void greeting(double &balance) {
cout << "Hello, thank you for banking with Pallet Town Bank.\n";
cout << "Please enter your name. ";
string name;
cin >> name;
cout << "Hello " << name << ". Your current balance is $" << moneyString(balance) << ".\n";
cout << "There will be a a service fee of $12.50 subtracted from your account.\n";
cout << "Your updated balance will be $" << moneyString(balance -= FEE) << " \n";
cout << "What would you like to do today?\n\n";
}
void printBalance(const double &balance) {
cout << "Current Balance is " << balance << '\n';
}
void withdraw(double &balance) {
cout << "Withdraw - How much would you like to withdraw? $";
int withdraw = getMoney();
cout << "Your new balance after withdrawing $" << withdraw << " will be $"
<< (balance -= withdraw -= FEE) << '\n';
}
void deposit(double &balance) {
cout << "Deposit - How much would you like to deposit? $";
int deposit = getMoney();
cout << "Your new balance after depositing $" << moneyString(deposit)
<< " will be $" << moneyString(balance += deposit -= FEE) << '\n';
}
int main()
{
// Initialize a sample session:
double balance = 157236;
greeting(balance);
while (true)
{
short int choice = getChoice();
if (choice == Choices::BALANCE) printBalance(balance);
else if (choice == Choices::WITHDRAW) withdraw(balance);
else if (choice == Choices::DEPOSIT) deposit(balance);
else if (choice == Choices::LOGOUT) break;
if (not userWantsMoreActions()) break;
}
cout << "Log Out - Thank you for banking with Pallet Town Bank. Have a great day!" << endl;
}
Live demo

Simple c++ ATM (can't store balance variable input)

I'm making a simple c++ atm program, but I'm having trouble with getting the balance to change after I make a deposit or withdraw.
// C++ ATM
#include "std_lib_facilities.h"
int main()
{
bool card_is_inserted = false;
double balance = 0.0;
//double new_balance = balance;
// HOME
//Starts over if variable is false
while (card_is_inserted == false)
{
cout << "Wellcome to Well's Fargo ATM " << '\n'
<< "Insert card Yes or No"<< endl;
string request;
getline(cin,request);
// Function is needed for aceppting different no's and yes's
//-=-=-=--=-==--=-=-==-==-=--==--=-=-
// loads atm
if (request == "yes")
{
cout << "Alright, Your current balance is:" << endl
<< balance << endl;
card_is_inserted = true;
}
// home
string option = "cancel";
while (card_is_inserted == true)
{
cout << "Would you like to withdraw or deposit? (Cancel)"<< endl;
getline(cin,option);
double cash_ = 0;
if (option == "deposit")
{
cout << "How much money would you like to deposit?" << endl;
cin >> cash_;
double new_deposit_balance = balance + cash_;
cout << "You placed: $" << cash_ << endl
<< "Your New Balance is: $" << new_deposit_balance << endl;
}
if (option == "withdraw")
{
cout << "How much money would you like to withdraw?" << endl;
cin >> cash_;
double new_witdraw_balance = balance - cash_;
if(balance <= 0)
{
cout << "You don't have: $" << cash_ << endl;
}
else
{
cout << "You toke: $" << cash_ << endl
<< "Your New Balance is: $"<< new_witdraw_balance << endl;
}
}
if (option == "cancel")
{
cout << "Ok, bye" << endl;
card_is_inserted = false;
}
}
}
}
example: I type yes to make a deposit(or withdraw) and then place a simple double like 12.50 then it shows me my current balance which will be 12.50; afterward I want to make a withdraw of 12.00 with .50 left. But I cant because the balance variable didn't store my previous value which was 12.50. I tried making "double new_balance = balance" but doesn't work like in swift.
You are not setting balance to new_witdraw_balance or new_deposit_balance.
double new_deposit_balance = balance + cash_; doesn't set the balance value because you are bring in the value of balance, but you are not assigning the outcome of balance + cash_ to balance.
You need to put something like balance = new_witdraw_balance; and balance = new_deposit_balance; at the end of each if after cout statement.
if(option == "deposit")
{
cout << "How much money would you like to deposit?" << endl;
cin >> cash_;
double new_deposit_balance = balance + cash_;
cout << "You placed: $" << cash_ << endl << "Your New Balance is: $" << new_deposit_balance << endl;
balance = new_deposit_balance; // this
}
if(option == "withdraw")
{
cout << "How much money would you like to withdraw?" << endl;
cin >> cash_;
double new_witdraw_balance = balance - cash_;
if(balance <= 0)
{
cout << "You don't have: $" << cash_ << endl;
}
else
{
cout << "You toke: $" << cash_ << endl << "Your New Balance is: $"<< new_witdraw_balance << endl;
balance = new_witdraw_balance; // and this
}
}
The line double new_deposit_balance = balance + cash_; only assigns the new balance to new_deposit_balance, but then you don't do anything with that variable (except print the value). If you want the new balance to persist, you actually need to modify balance, not new_deposit_balance, so something like balance = balance + cash_; or balance += cash_;.
The variable double new_deposit_balance only exists in the if-block it's defined in, so once you leave the if-block, you lose the information in new_deposit_balance. On the other hand, since balance is defined outside your if-blocks and while-loops, its value will persist throughout your ATM operations.
Of course, you'd also need to apply the same fix to new_witdraw_balance.

Cannot figure out how to do this code

I have been working on this project that simulates a bank account. The user can deposit, withdraw, and have all the withdraws and deposits displayed on the screen. At the top of the selection menu needs to be the current balance. Like so, SAVINGS : 100. And whenever I deposit money or withdraw money I need that balance to change to the correct amount. The amount starts at $100. If I deposit or withdraw money it will work perfectly the first time, but on the second time it gets reset back to $100 and then the transaction is done. How can I make the balance stay the correct amount without it resetting? This is a school project and I am not looking for someone to provide me the code. I am just seeking some tips or guidance in the right direction. Here is the code for my int main() :
int main ()
{
saving sa;
creditCard cca;
checking ca;
string n;
int option;
int exit = 1;
int x = 1;
do{
cout << endl;
cout << "Checking Balance:" << ca.getBalance() << " " << "Savings balance:" << sa.getBalance() << " " << "Credit Card balance:" << cca.getBalance() << endl;
cout << endl;
cout << " (1) Savings Deposit " << endl;
cout << " (2) Savings withdrawel " << endl;
cout << " (3) Checking Deposit " << endl;
cout << " (4) Write A Check " << endl;
cout << " (5) Credit Card Payment " << endl;
cout << " (6) Make A Charge " << endl;
cout << " (7) Display Savings " << endl;
cout << " (8) Display Checkings " << endl;
cout << " (9) Display Credit Card " << endl;
cout << " (0) Exit " << endl;
cin >> option;
switch ( option )
{
case 1 : {
double SamtD;
cout << " Please enter how much you would like to deposit into savings " << endl;
cin >> SamtD;
sa.makeDeposit(SamtD);
break;
};
case 2 : {
int SamtW;
cout << " Please enter how much you would like to withdraw "<< endl;
cin >> SamtW;
sa.doWithdraw(SamtW);
break;
}
case 3 : {
double CamtD;
cout << " Please enter how much you would like to deposit into checkings " << endl;
cin >> CamtD;
ca.makeDeposit(CamtD);
break;
}
case 4 : {
double CamtW;
int chkNum;
cout << " Please enter how much you wrote on the check " << endl;
cin >> CamtW;
cout << " Please enter the check number " << endl;
cin >> chkNum;
ca.writeCheck(chkNum, CamtW);
break;
}
case 5 : {
double CCmkP;
cout << " Please enter the amount you would like to deposit " << endl;
cin >> CCmkP;
cca.makePayment(CCmkP);
break;
}
case 6 : {
double DoC;
string Nm;
cout << " Please enter the amount charged to your credit card " << endl;
cin >> DoC;
cout << " Please enter where the charge was made " << endl;
cin >> Nm;
getline(cin, Nm);
cca.doCharge(Nm,DoC);
break;
}
case 7 : {
sa.display();
break;
}
case 8 : {
ca.display();
break;
}
case 9 : {
cca.display();
break;
}
case 0 : exit = 0;
break;
default : exit = 0;
cout << " ERROR ";
}
}
while(exit==1);
return 0;
}
and here is were the balance is being set :
double curbalance = 100;
void account::setBalanceD(double balance) // This is for deposit
{
itsBalance = balance;
}
void account::setBalanceW(double balance) // This is for withdraw
{
double newBalance = curBalance - balance;
itsBalance = newBalance;
}
double account::getBalance()
{
return itsBalance;
}
and here is the code that option 2 in my menu would be calling :
int saving:: doWithdraw(int amount)
{
if (amount > 0)
{
for (int i = 9; i != 0; i--)
{
last10withdraws[i] = last10withdraws[i-1];
}
last10withdraws[0] = amount;
setBalanceW(amount);
}
else
{
cout << " ERROR. Number must be greater then zero. " << endl;
}
return 0;
}
Any ideas? I just cannot get the balance to remain accurate.
The problem is that your setBalanceW() function never deducts from curBalance, so it stays at 100.
void account::setBalanceW(double balance) // This is for withdraw
{
curBalance -= balance;
}
Your other references should be to curBalance as well, as there should only be one current balance.
void account::setBalanceD(double balance) // This is for deposit
{
curBalance += balance;
}
double account::getBalance()
{
return curBalance;
}
The best solution, though (presuming that itsBalance is a member of the account class, would be to eliminate curBalance altogether, and just open your main() with an initial deposit:
int main ()
{
saving sa;
creditCard cca;
checking ca;
sa.makeDeposit(100.0);
/* other code */
}
/* Note no curBalance variable? */
void account::setBalanceW(double balance) // This is for withdraw
{
itsBalance -= balance;
}
void account::setBalanceD(double balance) // This is for deposit
{
itsBalance += balance;
}
double account::getBalance()
{
return itsBalance;
}
Your implementation for performing a deposit appears to be incorrect. It should be:
void account::setBalanceD(double balance) // This is for deposit
{
itsBalance += balance;
}
Notice that we are now adding the amount being deposited to the current balance, instead of explicitly setting it's value. Similarly as Ken pointed out the same applies to performing a withdraw.
void account::setBalanceW(double balance) // This is for withdraw
{
itsBalance -= balance;
}