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.
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):
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:
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:
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.