C++/Bank Accounts Program
I'm having some difficulty completing a program for one of my CS Courses. I've written all the code for the program, but I'm having difficulty with two files I must create. Below I've copied and pasted the directions, as well as the source code in my two .cpp files.
Basically, I need your assistance in getting the C++ source for the main function to work with the C++ source for the interest function. And I need some direction on how to create a header file for the external function.
Thanks in advance!
A bank updates t customers'' accounts at the end of each month. The bank offers two types of accounts: savings and checking. Every customer must maintain a minimum balance. If a customer's balance falls below the minimum balance, there is a service charge of $10.00 for savings accounts and $25.00 for checking accounts. If the balance at the end of the month is at least the minimum balance, the account receives interest as follows:
a) Savings accounts receive 4% interest.
b) Checking accounts with balances up to $5,000 more than the minimum balance receive 3% interest; otherwise, the interest is 5%.
The customer's account number (int type), account type (char; s for savings, c for checking), and current balance are input from the keyboard.
Output is the account number, account type, current balance (including any interest earned), and the amount of interest earned, if any.
The interest will be calculated in a separate function. The balance and interest rate are passed to the function. The function returns the interest earned.
The minimum account balance, for both checking and savings accounts, is $1,000. This must be declared as a constant.
Three files must be submitted:
C++ source for the main function
C++ source for the interest function
The header file created for the external function
using namespace std;
const double minimumBalance = 1000.00;
// Declare Variables
// For input, get the account number, account type, and current balance
cout << "Enter the Account Number: ";
cin >> acctNumber;
cout << "Account Type" << endl;
cout << "C = Checking Account" << endl;
cout << "S = Savings Account" << endl;
cout << "Enter the Account Type: ";
cin >> acctType;
cout << "Enter the Account Balance: ";
cin >> acctBalance;
if (acctType == 'S' && acctBalance < minimumBalance)
acctBalance = acctBalance - 10.00;
else if (acctType == 'C' && acctBalance < minimumBalance)
acctBalance = acctBalance - 25.00;
else if (acctType == 'S' && acctBalance >= minimumBalance)
interest = (acctBalance,4);
else if (acctType == 'C' && acctBalance >= 5000.00)
interest = (acctBalance,3);
else if (acctType == 'C' && acctBalance >= minimumBalance)
interest = (acctBalance,5);
cout << "Account Number: " << acctNumber;
cout << "Account Type: " << acctType;
cout << "Account Balance: " << acctBalance;
cout << "Amount of Interest Earned: " << interest;
interest = acctBalance * (interest / 100);
OK. Well you could start by actually creating the three files as asked for in the question – you appear to have two of the files so far.
You could then go back to your notes to see how to write a function declaration. Whilst you are about it you could also look up how to write a function definition – the code shown in Interest.cpp seems to be a single line of code. This is not valid C or C++.
As I have no intention of writing your coursework for you I shall provide examples of the form of a function – you may recognise this one – all C and C++ programs have such a function:
// code comprising function main
This is an unfair example as main has a few features not allowed for other functions. It returns an int but you do not have to explicitly return an int value – if no return statement is given the compiler implicitly adds a return value of 0 for you. So here is another function:
double pow( double x, double y )
double result = ...; // calculate x to the power y
Here I have left the calculation out and just placed a ... to show that some real code should be placed here to calculate x to the power y. pow is part of the C standard library and therefore also part of the C++ standard library. The function is declared in math.h – although for C++ you should include cmath.
If you look in math.h you will find a line like so:
double pow(double, double);
or similar. It may have some additional fancy implementation specific parts but this is the plain and usual way to declare a function. In fact you can also add in the parameter names – as in the function definition - if you like so we could have declared the pow function like so:
double pow( double x, double y );
I like to do this as I can copy and paste the declaration from the definition or vice versa – adding or removing the semicolon as necessary. It also gives more of a hint to users of your function as to what the parameters are, so they can pass the correct values.
Note that a function declaration is also known as a function prototype (from C).
So what are the parts of the function declaration? Well first we have the type of the returned value – or void if no value is returned (such a function might be called a procedure in other programming languages). Next we have the name of the function, then in ( and ) we have the types and names of the parameters we need to pass to the function when it is called – so the example above is a function called pow that takes two double parameters and returns a double result:
double fourCubed = pow( 4.0, 3.0 );
So what is the difference between a declaration and a definition?
Well a declaration just says the item declared exists. It does not say how it is made up. In C and C++ you can declare something exists as many times as you like – so long as all the declarations are consistent.
A definition on the other hand says this is how a thing is made up. In C and C++ things can only have one definition.
In C and C++ most definitions are also declarations, and this is true of functions – so my definition of pow also declares that it exists. However the compiler likes to know of a things existence before you use it. This means either all places a function like pow is called have to have the definition of pow available before it is called – which would force those definitions to be placed some place before all calls – such as at the top of a source file or in a header file included at the top of the source file and except in simple cases would result in multiple definitions of the function being compiled which is illegal. Or we use a separate function declaration and use this instead so all places that call pow only have to know of the declaration and not the full definition. To allow this to be shared among source files such declarations are placed in header files – as you are requested to do here – which may then be included at the top of source files that have calls to the declared functions. So for the pow example you include math.h (or cmath for standard C++ implementations).
Hope this moves you forward a bit. You might like to actually compile your program code – the compiler will complain about things it does not like, as will the linker used to link the two parts of your program plus runtime libraries together to form an executable. Oh, remember that if your program builds (i.e. produces a executable application) it just means you got the language right and consistent not that what you wrote will make any sense when run.