You are here:

C++/Bank Accounts Program

Advertisement


Question
Hi,

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!  

-----------
Directions:
-----------

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
------------------------------------

-----------------
Bank_Accounts.cpp
-----------------
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const double minimumBalance = 1000.00;

int main()
{
  // Declare Variables
  int acctNumber;
  char acctType;
  float acctBalance;
  double interest;

  // 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.cpp
------------
interest = acctBalance * (interest / 100);

Answer
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:

       int main()
       {
         // 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
         return result;
       }

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.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.