C Worksheet

Before we start getting into the meat of C++ (in the second session) its worth making sure that our knowledge of basic C is still firm. That means knowing about: variables, arithmetic, control structures and functions. The more difficult bits of the language will be covered again in the C++ course again anyway (pointers, arrays, strings and objects).

If you are feeling a little rusty you can try and work your way through the following exercises, they slowly build up a simple bank account program. N.B. These are still C++ programs, they use cin and cout, but they do not contain any classes or objects.

The if/else Structure

Try and get the following program to compile in Borland C++:

#include <stdio.h>
int main(void)
{
	int balance, adjust;

	cout << "What is the current balance? ";
	cin >> balance;

	cout << "\nWhat amount to you wish to deposit? ";
	cin >> adjust;

	balance += adjust;

	cout << "\nYour new balance is : " << balance << "ukp";
	return 0;
}

This is a pretty basic (and pretty poor!) bank program. We are going to improve the program by allowing the user to make withdrawals as well. Make the following changes (do one at a time, and make sure that each one compiles and does what you intended before you move onto the next):

  1. Add a cout statement that asks the user what they want to do (1. a withdrawal or 2. a deposit), allow them to type in an answer by adding a cin statement that reads in a number
  2. Add in an if/else structure that checks their answer it will look something like this:

    if(choice == 1) /*make a withdrawal*/
    else /*make a deposit*/


  3. You may have noticed that if you type a negative number in either case then you will get the opposite effect you desired. Add a check into the program (you will need one check for each of the two cases) that makes sure that the user has not entered a negative number (N.B. if they have then you can change it back to a positive by multiplying by -1)

Loops

The problem with our program is that it only allows us to make a single deposit or withdrawal. The following program uses a do...while loop to allow the user to go around a simple program again. Can you achieve the same effect in your bank program, add a similar do...while loop that allows a user to make multiple transactions?


#include <iostream.h>

int main(void)
{
	int first, second, again;

	do
	{
		cout << "type first number >";
		cin >> first;
		cout << "\ntype second number >";
		cin >> second;

		cout << "\nadded together they equal : " << first+second;

		cout << "\nDo you want another go? (type 0 to end, 1 to continue) >";
		cin >> again;

	} while(again != 0);

	return 0;
}

One of the advantages of loops is that we can do cumulative arithmetic very easily, such as calculating interest over a number of years. Try adding a third option to your program, instead of depositing or withdrawing money this will calculate and add interest over a number of years. You can do this as follows:

  1. ask the user what the yearly interest rate is (e.g 0.08 is equal to 8%), store it in a float
  2. ask the user how many years they want to leave their money, store it in an int
  3. create a for loop that goes around once for each year
  4. on each iteration of the loop multiply the balance by the interest rate and add it to the balance

Functions

Now we have a program that is useful (at least slightly!) we should worry about splitting it up into functions. The following code is the same as the very first example, but this time it has taken the deposit code out of main and put it into a function. N.B. the way that the balance is passed into the function, changed and then returned back out again.

#include <iostream.h>

int deposit(int orig_amount);

int main(void)
{
	int balance;

	cout << "What is the current balance? >";
	cin >> balance;

	balance = deposit(balance);

	cout << "\nYour new balance is : " << balance;
	return 0;
}

int deposit(int orig_amount)
{
	int adjust;

	cout << "\nWhat amount to you wish to deposit? >";
	cin >> adjust;

	orig_amount += adjust;

	return orig_amount;
}

Try incorporating this deposit function into your code and then try to:

  1. Create a display function that prints the deposit on the screen in a pleasing manner
    void display(int amount);
  2. Create a functon for the withdrawal functionality
    int withdrawal(int amount);
  3. Create a function for the interest functionality
    int interest(int amount);

Remember to implement one function at a time and to declare a prototype for each at the top of the program, beneath the #include statement.

C++

In the first session of the C++ course we looked at how to define C++ objects and encapsulate data inside them. If you have time why not try rewriting this bank account example as a C++ class. Instantly we have the advantage that we can create multiple different accounts.