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"
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);
}
You are not updating the balance variable. Instead you are assigning to the test variable.
An unrelated problem that I just happened to notice is with your use of the || operator. if (answer == 'y' || 'Y') should be written as if (answer == 'y' || answer == 'Y') otherwise 'Y' will be treated as true (because it's not a zero value) making the whole expression always true.
I was just messing with you. All the loop types can be interchanged; the difference is how clean the code is. For example, do-while can be done with (do it once block followed by while block). A for loop with only a condition is the same as a while loop. A while loop that modifies the loop variable inside the body is the same as a for loop. Youll see as you get into them.
for loops are not too bad.
for( statements; condition; statements)
{}
USUALLY they are of the form
for(int i = 0; i < something; i++) //the statements are usually ONLY used for the control variables.
but its perfectly legal to do other stuff in the statements. Its not great style. This is the classic for loop.
there is also another format that you will learn later for iterating containers, called a ranged based for loop. looks like for(type &variable:range) ... there are a bunch of similar but different ways to write these, depending on what you are doing, you can look at them later.