0

Here is my unaltered working code:

#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int interest(int Balance, int MAXACCOUNTS);
struct Account
{
int Number;
double Balance;
int DaysSinceDebited;
};

int main()

{
int Accountnumber;
double Balance;
int DaysSinceDebited;
double Total[MAXACCOUNTS] = {};


Account accounts[MAXACCOUNTS];

accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;

accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;

accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;

accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;

accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;

accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;

accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;

accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;





for (int i = 0; i < MAXACCOUNTS; i++)
{

if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited>30))
    Total[i] = accounts[i].Balance * 1.06; //6% interest added
else Total[i] = accounts[i].Balance * 1.03; //3% interest added
cout << accounts[i].Number << " has a balance of " << accounts[i].Balance <<  ". The amount with interest is: " << Total[i] << endl;

system("pause");
}
}

Here is what i need to do: You must add a function to your program called CalcInterest. This function will take as its ONLY parameter an Account, and return the Interest calculated as shown in Part 1. Your main program should now use this function instead, to generate the display as in Part 1.

This is what i tried:

#include <iostream>

using namespace std;

 const int MAXACCOUNTS = 8;
 int CalcInterest(Account);
 struct Account { //declare struct outside of main

int Number;
double Balance;
int DaysSinceDebited;

};



int main()

{

int AccountNumber[MAXACCOUNTS] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };

double Balance[MAXACCOUNTS] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };

int DaysSinceDebited[MAXACCOUNTS] = { 20, 35, 2, 14, 5, 360, 1, 45 };
double Total[MAXACCOUNTS] = {};
//add your code here



Account accounts[MAXACCOUNTS];

accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;

accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;

accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;

accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;

accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;

accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;

accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;

accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;



CalcInterest(Account);

}


int CalcInterest(Account) {

for (int i = 0; i < MAXACCOUNTS; i++)
{

    if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited > 30))

        Total[i] = accounts[i].Balance * 1.06;
    else Total[i] = accounts[i].Balance * 1.03;
    cout << accounts[i].Number << "has a balance of " << accounts[i].Balance  << ". The amount with interest is : " << Total[i] << endl;
}
system("pause");
return 0;

}

There were many errors with this, mostly of things becoming undefined, such as .DaysSinceDebited etc PLEASE HELP!

4
  • 3
    I'm voting to close this question as off-topic because it asks to program for OP Commented Sep 1, 2015 at 0:09
  • No, they just don't understand the compilation errors they are getting.. It would have been helpful to show those errors, but all they are really asking for help with is some misunderstandings they have with refactoring. Commented Sep 1, 2015 at 0:11
  • 'accounts' is undefined, 'total' is undefined, main local functions are illegal, there are a good few more of these, im looking for the correct way to refactor this Commented Sep 1, 2015 at 0:17
  • 1
    For future reference, if you are getting compilation errors that you don't understand, it's crucial that you put those in your question. Commented Sep 1, 2015 at 0:23

1 Answer 1

1

I believe that by "there are many errors", you are talking about compilation errors. A quick look at your code confirms this.

The first mistake you made is that this function only works on a single account. You therefore cannot loop over your accounts array inside that function, neither can you access Total. You are also a little confused about the syntax of passing arguments, as well as the datatype that should be returned. I can help you with that.

Change your function definition to:

double CalcInterest( const Account & account )
{
    // Do your interest calculation on 'account' here, then return it from the function.
    double interest = 0.0;  //<-- For you to do.
    return interest;
}

Then you can simplify your loop in main...

for (int i = 0; i < MAXACCOUNTS; i++)
{
    // Calculate the interest on the account, then do something with it.
    double interest = CalcInterest( accounts[i] );
    Total[i] = 0.0;  //<-- For you to do.
}

Note that I've only provided the language structure here, since this is obviously an assignment of some sort. I have indicated the parts where you need to do some work.

Sign up to request clarification or add additional context in comments.

13 Comments

Thankyou will have a go at it now!
double CalcInterest( const Account & accounts) { for (int i = 0; i < MAXACCOUNTS; i++) { if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited>30)) double Total[i] = CalcInterest(accounts[i].Balance * 1.06); //6% interest added else double Total[i] = CalcInterest(accounts[i].Balance * 1.03); //3% interest added cout << accounts[i].Number << " has a balance of " << accounts[i].Balance << ". The amount with interest is: " << Total[i] << endl; system("pause"); return Total; } } That is what i tried and still have a ton of errors which i will also post
I think you need to read my answer again. Then read it three more times.
the task says " This function will take as its ONLY parameter an Account, and return the Interest calculated " so is double CalcInterest( const Account & account ) okay to use?
Yes, that takes ONLY an account. You can pass by value (Account account) if you want, but I am passing a const reference which is how you normally do it in C++. The return type of double is correct.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.