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;
}
Related
I was tasked to create an ATM mock program and my problem is overwriting the money and PIN variables with the information that the user will enter.
Here it is:
#include <iostream>
using namespace std;
void Check(int money) { cout << "Your current balance is: " << money << endl; }
void Deposit(int money) {
int deposit;
cout << "Please enter the amount of cash you wish to deposit.\n";
cin >> deposit;
money += deposit;
cout << "Your new balance is: " << money << endl;
}
void Withdraw(int money) {
int withdraw;
cout << "Please enter the amount of cash you wish to withdraw.\n";
cin >> withdraw;
money -= withdraw;
cout << "Your new balance is: " << money << endl;
}
void Interest(int money) {
money += money * 0.05;
cout << "Your money with interest is: " << money << endl;
}
void Penalty(int money) {
if (money < 5000) {
money -= money * 0.02;
cout << "Your penalty is: " << money << endl;
} else
cout << "Your account will not incur a penalty because you are above the "
"minimum threshold.\n";
}
void ChangePIN(int PIN) {
int p;
cout << "Enter a new PIN: ";
cin >> p;
PIN = p;
cout << "Your new PIN is: " << PIN << endl;
}
int main() {
int money = 5000, PIN = 1234, EPIN;
cout << "Enter your PIN (Default PIN is 1234): \n";
cin >> EPIN;
if (EPIN == PIN) {
int choice;
cout << "Welcome!\n"
<< "1 - Check available balance \n"
<< "2 - Deposit cash \n"
<< "3 - Withdraw cash \n"
<< "4 - Compute for the interest of your account(5%)\n"
<< "5 - Compute for the penalty of having a balance below 5000 (2%) \n"
<< "6 - Change your PIN\n"
<< "7 - Exit\n"
<< "Your choice: ";
cin >> choice;
switch (choice) {
case 7: {
break;
}
{
case 1: {
Check(money);
break;
}
case 2: {
Deposit(money);
break;
}
case 3: {
Withdraw(money);
break;
}
case 4: {
Interest(money);
break;
}
case 5: {
Penalty(money);
break;
}
case 6: {
ChangePIN(PIN);
break;
}
}
}
return 0;
}
}
As you can see I'm pretty much a beginner at this. My problem is the money and PIN have the default values of 5000 and 1234 respectively. Now, I need to make the user be able to change these values but once I use return main() they get assigned the same starting values again, What would be the best workaround for this? I thought of using some sort of accumulator for this but I'd like some advice first.
You can simply do this by using a while loop.
Run an infinite while loop and break it whenever you want to exit from the program.
Here is the code:
#include <iostream>
using namespace std;
void Check(int money) { cout << "Your current balance is: " << money << endl; }
void Deposit(int money) {
int deposit;
cout << "Please enter the amount of cash you wish to deposit.\n";
cin >> deposit;
money += deposit;
cout << "Your new balance is: " << money << endl;
}
void Withdraw(int money) {
int withdraw;
cout << "Please enter the amount of cash you wish to withdraw.\n";
cin >> withdraw;
money -= withdraw;
cout << "Your new balance is: " << money << endl;
}
void Interest(int money) {
money += money * 0.05;
cout << "Your money with interest is: " << money << endl;
}
void Penalty(int money) {
if (money < 5000) {
money -= money * 0.02;
cout << "Your penalty is: " << money << endl;
} else
cout << "Your account will not incur a penalty because you are above the "
"minimum threshold.\n";
}
int ChangePIN(int PIN) {
int p;
cout << "Enter a new PIN: ";
cin >> p;
cout << "Your new PIN is: " << PIN << endl;
return p;
}
int main() {
int money = 5000, PIN = 1234;
while(1){ // run an infinite loop
int EPIN;
cout << "Enter your PIN (Default PIN is 1234): \n";
cin >> EPIN;
if (EPIN == PIN) {
int choice;
cout << "Welcome!\n"
<< "1 - Check available balance \n"
<< "2 - Deposit cash \n"
<< "3 - Withdraw cash \n"
<< "4 - Compute for the interest of your account(5%)\n"
<< "5 - Compute for the penalty of having a balance below 5000 (2%) \n"
<< "6 - Change your PIN\n"
<< "7 - Exit\n"
<< "Your choice: ";
cin >> choice;
switch (choice) {
case 7: {
return 0; // breaking condition
}
{
case 1: {
Check(money);
break;
}
case 2: {
Deposit(money);
break;
}
case 3: {
Withdraw(money);
break;
}
case 4: {
Interest(money);
break;
}
case 5: {
Penalty(money);
break;
}
case 6: {
PIN = ChangePIN(PIN);
break;
}
}
}
}
}
return 0;
}
Does this answer your question?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
#include <iostream>
using namespace std;
// ATM menu
void Menu()
{
cout << " MENU " << endl;
cout << "1.Deposit" << endl;
cout << "2.Balance" << endl;
cout << "3.Withdraw" << endl;
cout << "4.Transfer" << endl;
cout << "5.Exit" << endl;
cout << "-------------------------------" << endl;
}
// Tasks of ATM
int Task(int balance, int balance2, int option)
{
int amount;
cin >> option;
switch(option)
{
case 1:
cout << "please enter the amount" << endl;
cin >> amount;
balance += amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
case 2:
cout << balance << endl;
break;
case 3:
cout << "please enter the amount" << endl;
cin >> amount;
if(amount <= balance)
{
balance -= amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
}
else
cout << "insufficent amount";
break;
case 4:
cout << "please enter the amount" << endl;
cin >> amount;
if(amount <= balance)
{
balance -= amount;
balance2 += amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
}
else
cout << "insufficent amount";
break;
}
}
// Main func
int main1()
{
cout << "Please choose Your account " << endl;
int Account1, Account2;
cout << "Account 1 , Account 2" << endl;
char x;
cin >> x;
int option;
// Account 1 lines:Balance = 500 , balance 2= 700
do
{
if(x == Account1)
{
Menu();
cout << "option:" << endl;
cin >> option;
Task(500, 700, option);
}
// Account 2 lines : Balance = 700 , balance 2= 500
else
{
int option;
cout << "option:" << endl;
cin >> option;
Task(700, 500, option);
}
} while(option != 5);
}
im new to c++ and i did some coding which looks like like this and when i try to build the executable file i get this error: no return statement in function returning non void [-Wreturn-type] in line 40 and 64.i really have no idea what the problem is and i searched alot in the internet to understand what it is but i didn't understand the explanations at all.
It's a really simple solution. You created int function that never returns a value. If you have a function that you don't want any value to be returned, just make it void. To make your code work simply change function type from int to void
void Task(int balance, int balance2, int option)
The second issue is that you did declare balance1 and balance2, but you forgot to declare their values which lead to another error ( how is the compiler supposed to know their starting value?):
int Account1; Account2; // incorrect
int Account1=0, Account2=0; //fixed
Of course, you can also set these values later, but in this case, you should do it while declaring.
Another thing, why do you have int main1() function?
compiler will not treat it as you desire - it has to be called int main()
in order to do what you want.
You are missing "return balance" at the end of the Task() function. You define your function to return integer, that means you have to have return statement inside the function. Also, you have to have main() function, not main1() as in your case. Every C/C++ needs function that is called main() and it represent the entry point of the program. Try this code:
#include <iostream>
using namespace std;
// ATM menu
void Menu() {
cout << " MENU " << endl;
cout << "1.Deposit" << endl;
cout << "2.Balance" << endl;
cout << "3.Withdraw" << endl;
cout << "4.Transfer" << endl;
cout << "5.Exit" << endl;
cout << "-------------------------------" << endl;
}
// Tasks of ATM
int Task(int balance, int balance2, int option) {
int amount;
cin >> option;
switch (option) {
case 1:
cout << "please enter the amount" << endl;
cin >> amount;
balance += amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
case 2:
cout << balance << endl;
break;
case 3:
cout << "please enter the amount" << endl;
cin >> amount;
if (amount <= balance) {
balance -= amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
} else
cout << "insufficent amount";
break;
case 4:
cout << "please enter the amount" << endl;
cin >> amount;
if (amount <= balance) {
balance -= amount;
balance2 += amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
} else
cout << "insufficent amount";
break;
}
return balance;
}
// Main func
int main() {
cout << "Please choose Your account " << endl;
int Account1, Account2;
cout << "Account 1 , Account 2" << endl;
char x;
cin >> x;
int option;
// Account 1 lines:Balance = 500 , balance 2= 700
do {
if (x == Account1) {
Menu();
cout << "option:" << endl;
cin >> option;
Task(500, 700, option);
}
// Account 2 lines : Balance = 700 , balance 2= 500
else {
int option;
cout << "option:" << endl;
cin >> option;
Task(700, 500, option);
}
} while (option != 5);
}
In the output, I keep getting -"dollars" and -"cents" instead of positives as shown in the picture.
I have used a class named savings account to set initial balance, deposit and withdraw. And I am asked to use 1 object that prompts the user for input and another that uses overload constructor to initialize dollar and cents.
// Include Section
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class SavingsAccount
{
public:
SavingsAccount();
SavingsAccount(int, int);
void setInitial(int, int);
void setDeposit(int, int);
void setWithdraw(int, int);
void output();
private:
int dollars;
int cents;
};
// Main Function
int main()
{
// object declaration
// Bank 1
SavingsAccount bank1; //has its values set during definition by the user
//Bank 2
SavingsAccount bank2(200, 50); //uses the constructor to store values into member variables
bank2.setDeposit(40, 50);
bank2.setWithdraw(100, 98);
bank2.output();
cout << "\n\n";
// Variable declaration
string repeat;
int d, c;
int choice;
// Prompt for initial balance
cout << "welcome to your savings account! Please fill in the appropriate information.\n\n";
cout << "Initial balance (dollars): ";
cin >> d;
cout << "Initial balance (cents): ";
cin >> c;
bank1.setInitial(d, c);
do
{
cout << "Pick an option: " << endl
<< "1. Deposit money" << endl
<< "2. Withdraw money" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Deposit amount (dollars): ";
cin >> d;
cout << "Deposit amount (cents): ";
cin >> c;
bank1.setDeposit(d, c);
break;
case 2:
cout << "Withdraw amount (dollars): ";
cin >> d;
cout << "Withdraw amount (cents): ";
cin >> c;
bank1.setWithdraw(d, c);
break;
case 3:
break;
default:
while (choice != 1 && choice != 2)
{
cout << "Invalid choice, enter 1 or 2: \n";
cin >> choice;
}
}
// Display output
bank1.output();
// Prompt for continuing
cout << "\nWould you like to keep going? (y or Y for yes)";
cin >> repeat;
}while (repeat == "y" || repeat == "Y");
cout << "End Program.\n\n";
return 0;
}
SavingsAccount::SavingsAccount()
{
dollars = 0;
cents = 0;
}
SavingsAccount::SavingsAccount(int newD, int newC)
{
dollars = newD;
cents = newC;
cout << "Your initial balance is $" << dollars << "." << cents << endl;
}
void SavingsAccount::setInitial(int initialD, int initialC)
{
while (initialC >= 100)
{
initialC = initialC - 100;
initialD++;
}
dollars = initialD;
cents = initialC;
cout << "Your initial balance is $" << dollars << "." << cents << endl;
}
void SavingsAccount::setDeposit(int depD, int depC)
{
while (depC >= 100)
{
depC = depC - 100;
depD++;
}
dollars = depD + dollars;
cents = depC + cents;
cout << "Depositing $" << depD << "." << depC << " to your account...\n";
}
void SavingsAccount::setWithdraw(int withD, int withC)
{
while (withC >= 100)
{
withC = withC - 100;
withD++;
}
if (withD > dollars)
{
cout << "Not enough money to be withdrawn.\n";
}
else
{
dollars = withD - dollars;
cents = withC - cents;
cout << "Withdrawing $" << withD << "." << withC << " from your account...\n";
}
}
void SavingsAccount::output()
{
cout << "dollars = " << dollars << " cents = " << cents << endl;
}
You ask How can I find the mistake I am making in my C++ code? (and not What is the mistake I am making in my C++ code?)
Part of the answer to your question is, and this is especially applicable to small programs when you first start learning to program, to trace the execution of your program with pencil and paper. Then, as your programs become longer and more complicated, start watching their execution with a debugger.
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();
Getting an unassigned variable error after the GetTransfer portion of the code. It is claiming account1 is the unassigned variable but from what i can see i have declared it. i have tried multiple times to correct it and i just can figure it out.
#include <iostream>
#include <conio.h>
using namespace std;
// Chance Pinkerton
// December 3, 2013
// CaseProject2Chapter3
struct bankInfo
{
int accountNum;
double startBal;
double endBal;
};
int main()
{
bankInfo account1;
bankInfo account2;
double transferAmt;
int GetAccount1();
int GetAccount2();
double GetBal1();
double GetBal2();
double GetTransfer();
account1.accountNum = GetAccount1();
while (account1.accountNum < 1000 || account1.accountNum > 9999)
{
cout << "Error. That account does not exsist." << endl;
account1.accountNum = GetAccount1();
}
GetBal1();
account2.accountNum = GetAccount2();
while (account2.accountNum < 1000 || account2.accountNum > 9999)
{
cout << "Error. That account number doesnt exsist." << endl;
account2.accountNum = GetAccount2();
}
if (account2.accountNum == account1.accountNum)
{
cout << "Error. Account numbers can not be the same " << endl;
account2.accountNum = GetAccount2();
}
GetBal2();
transferAmt = GetTransfer();
account1.accountNum = (account1.accountNum % 5) + (account1.accountNum * 10);
account2.accountNum = (account2.accountNum % 5) + (account2.accountNum * 10);
account1.endBal;
account2.endBal;
while (account1.endBal < 0)
{
cout << "Error. Account balance can not be negative." << endl;
GetTransfer();
if (account1.endBal < 10)
{
cout << "Warning. Account balance will be below $10.00 " << endl;
}
}
account1.endBal = account1.startBal - transferAmt;
account2.endBal = account2.startBal + transferAmt;
cout << "Account number 1: " << account1.accountNum << endl;
cout << "Starting balance: " << account1.startBal << endl;
cout << "Ending balance: " << account1.endBal << endl;
cout << "Account number 2: " << account2.accountNum << endl;
cout << "Starting balance: " << account2.startBal << endl;
cout << "Ending balance: " << account2.endBal << endl;
getch();
return 0;
}
int GetAccount1()
{
int accountNum;
cout << "Please enter your account number " << endl;
cin >> accountNum;
return accountNum;
}
int GetAccount2()
{
int accountNum;
cout << "Please enter the second account number " << endl;
cin >> accountNum;
return accountNum;
}
double GetBal1()
{
double startBal;
cout << "Enter your account balance " << endl;
cin >> startBal;
return startBal;
}
double GetBal2()
{
double startBal;
cout << "Enter the second account balance " << endl;
cin >> startBal;
return startBal;
}
double GetTransfer()
{
double transferAmt;
cout << "How much would you like to transfer to the second account " << endl;
cin >> transferAmt;
return transferAmt;
}
These two statements do exactly nothing, and are likely the source of the warning the compiler is giving you:
account1.endBal;
account2.endBal;
Furthermore, I think you need to actually capture the return value of GetTransfer() here, perhaps into transferAmt:
GetTransfer();
You probably need to rethink this entire while loop, especially taking into account how transferAmt relates to account1.endBal:
while (account1.endBal < 0)
{
cout << "Error. Account balance can not be negative." << endl;
GetTransfer();
if (account1.endBal < 10)
{
cout << "Warning. Account balance will be below $10.00 " << endl;
}
}
For example, you don't want to update endBal until you know the transfer will succeed. Furthermore, your low-balance warning is in completely the wrong place.