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:
};

Edited 4 Years Ago by faintdeath: n/a

Delete lines 14 and 15 and see if the program compiles. If it does, then you know there's a problem. Besides, you initialize balance in line 14, then create the object in line 18.

I'm seeing a problematic lack of a default constructor for this class. If you care what the program initializes everything to(which you clearly do), you should create one. Otherwise the program is free to initialize balance to 92 or anything else it wants.

Delete lines 14 and 15 and see if the program compiles. If it does, then you know there's a problem. Besides, you initialize balance in line 14, then create the object in line 18.

I'm seeing a problematic lack of a default constructor for this class. If you care what the program initializes everything to(which you clearly do), you should create one. Otherwise the program is free to initialize balance to 92 or anything else it wants.

thanks VernonDozier, for the quick response. the program does compile when i delete line 14 and 15 but im not sure how to create or use a constructor. can anybody tell me what i should change for the program or what i should add to make it so that the balance starts $0 instead any random number.

You're welcome. Delete lines 14 and 15 of account.cpp. They have nothing to do with you class variable called balance, which is the only balance variable you want.

As for the constructor, stick this in the "public" portion of your class in account.h below line 14.

Account()
{
    // stick code here
}

When line 18 of account.cpp...

Accout account1;

executes, the Account() constructor is called. Stick anything that needs to be initialized in there...

Account()
{
    cout << "Account() has been called.\n"; // delete this line.  I just stuck it in here
                                            // so you can see when it is called.
    balance = 0;  // this is the line you need to initialize things.  Initialize anything 
                  // else inside this function too.
}

Edited 4 Years Ago by VernonDozier: Fix grammar.

it works perfectly now tyvm! was tryin to figure it by reading my c++ book but it sounded more confusing than wa you showed. really helps out =)

This question has already been answered. Start a new discussion instead.