The purpose of the program is to create an account where you can deposit, withdraw, and check your balance. I have to use class and put it into a project with a .cpp and .h file. When i compile this it works almost perfect, but the balance always starts at $92 when i already initialized the balance to $0 in the .cpp file. Here are the .cpp and .h text files. Any help or suggestions are very much appreciated. Thank You!
//account.cpp
#include<iostream>
#include<string>
#include "account.h"
using namespace std;
int main()
{
int action;
int deposit;
int withdraw;
int balance;
balance = 0;
Account account1;
while(action != 4)
{
cout << "Welcome to your bank account. What action do you want to perform to your account? \n";
cout << "1 - Deposit money to account.\n"; //adds an amount to the current balance
cout << "2 - Withdraw money from account.\n"; //takes out an amount
cout << "3 - Check balance.\n";
cout << "4 - I am done with the account.\n";
cout << "Enter the number of the choice of action you wish to perform.\n";
cin >> action;
switch(action)
{
case 1:
account1.credit();
break;
case 2:
account1.debit();
break;
case 3:
account1.getBalance();
break;
case 4:
cout << "You are now done with the transaction. Have a nice day.\n";
break;
default:
cout << "The choice you have chosen is invalid. Please choose another option.\n";
break;
}
}
system("PAUSE");
return 0;
}
//account.h
#include<iostream>
#include<string>
using namespace std;
class Account
{
int action;
int deposit;
int withdraw;
int balance;
public:
float credit()
{
cout << "How much money do you wish to deposit into your account?\n";
cin >> deposit;
balance = balance + deposit;
cout << "The current balance is $" << balance << " after the transaction.\n";
}
float debit()
{
cout << "How much money do you wish to withdraw from your account?\n";
cin >> withdraw;
balance = balance - withdraw;
if(balance < 0)
{
cout << "Debit amount exceeded account balance. Please choose another option.\n";
balance = balance + withdraw;
}
else
{
cout << "The current balance is $" << balance << " after the transaction.\n";
}
}
void getBalance()
{
cout << "Your current balance for your account is $" << balance << '\n';
}
private:
};