Andrew Wilson
Andrew Wilson

Reputation: 23

Function not returning correct result

const int CHECK_MAX = 50;

void balanceAccount(double account)
{
    double start = 0;
    double interest = 0;
    double check[CHECK_MAX];

    account = (start - check[CHECK_MAX]) * (1 + interest);
    cout << account;
}

int main()
{
    int sub;
    double start = 0;
    double interest = 0;
    double total = 0;
    double check[CHECK_MAX];
    void balanceAccount(double);

    cin >> start;

   for (sub = 0; sub < CHECK_MAX; sub++)
    {
        cin >> check[sub];

        if (check[sub] == 0)
        {
            cout << "Thank you." << endl;
            break;
        }
    }

   cin >> interest;

    if (interest == 0)
    {
        balanceAccount(total);
    }
        else
    {
            balanceAccount(total);
    }

    return 0;
}

The purpose of this program is to prompt the user to enter a starting balance for a bank account, and then using an array, enter the individual amount for checks(up to 50) and have them subtracted from the initial balance. Then, if an interest value exists, that is multiplied to the total. The ending result is computed with the balanceAccount() function. I believe everything is working decently until the final computation. I keep getting the incorrect calculation. I'm eventually going to include a currency format to it but for now I just want it to compute correctly. Thank you for your time.

Upvotes: 0

Views: 88

Answers (4)

HadeS
HadeS

Reputation: 2038

change balanceAccount() method as below..

double balanceAccount(char account[])
{
    double start = 0;
    double interest = 0;
    double check[CHECK_MAX];

    double account = (start - check[CHECK_MAX-1]) * (1 + interest);
    cout << account;
}

For main balanceAccount()

total = balanceAccount(check);

Reason for passing check in balanceAccount();you have created different check's in main() and balanceAccount() and both are not same...

Also check[CHECK_MAX] operation is not valid... 0 to CHECK_MAX-1 are the valid memory location which counts upto CHECK_MAX...changed according in the code above

Upvotes: 0

Vishal Santharam
Vishal Santharam

Reputation: 2023

Working code based on your statement Try this,

  #include<iostream>
using namespace std;

const int CHECK_MAX = 50;

void balanceAccount(double total,double interest)
{
    double account = (total) * (interest);
    cout << account;
}

int main()
{
    int sub;
    double start = 0;
    double interest = 0;
    double total = 0;
    double check[CHECK_MAX];
    void balanceAccount(double,double);

    cout << "Enter the beginning balance of the account at the beginning of the month." << endl;
    cin >> start;
    total = start;
    cout << "Enter the individual dollar amount of checks written this month." << endl;
    cout << "A maximum number of 50 checks is enforced." << endl;
    cout << "Enter a zero when you are finished entering check values." << endl;
    for (sub = 0; sub < CHECK_MAX; sub++)
    {
        cout << "Enter the dollar amount for the checks written, but one at a time." << endl;
            cin >> check[sub];
        total = total - check[sub];
        if (check[sub] == 0)
        {
            cout << "Thank you." << endl;
            break;
        }
    }

    cout << "If applicable, enter an interest rate." << endl;
    cout << "If there is no interest rate, enter a 0." << endl;
    cin >> interest;
    if(interest != 0)
    {
        cout << "Thank you." << endl;
        balanceAccount(total,interest);
    }
    else
    {
        cout << "Thank you." << endl;
        cout << total << endl;
    }
    return 0;
}

Upvotes: 0

Blue Ice
Blue Ice

Reputation: 7922

All of the variables in balanceAccount() are being initialized to zero, so the other variables in main() have no effect. To remedy this, pass the variables to balanceAccount() :

void balanceAccount(double localAccount, double localStart, double localInterest, double localCheck[])
{
    account = (start - check[CHECK_MAX]) * (1 + interest);
    cout << account;
}

You might also want to return account.'

This line:

account = (start - check[CHECK_MAX]) * (1 + interest);

goes past the end of the array, it should be:

account = (start - check[CHECK_MAX-1]) * (1 + interest);

I also don't know what you are doing with the if...else at the end of main().

Upvotes: 0

John3136
John3136

Reputation: 29266

double check[CHECK_MAX];

account = (start - check[CHECK_MAX]) * (1 + interest);

Accessing check[CHECK_MAX] is not valid - it's running off the end of the array. C++ arrays are indexed 0-n-1, so the last item in the array is check[CHECK_MAX-1].

Upvotes: 1

Related Questions