Floating Point Numbers and Strings Revisited

Floating Point Numbers

So far we have defined floating point numbers (numbers with something after the decimal point) as type "float". For general C++ programming this is acceptable. The official AP guidelines, however, tell us that we should be defining these numbers as type "double". The difference between a double and a float is that a double can hold more numbers after the decimal point. The drawback of this is that it takes up more memory, but we won't be concerned with that here. Implementing this change is easy, all you have to do is replace the word "float" with the word "double". For example instead of using this code fragment:

float pi;

We would use this one

double pi;

From now on, this is the style that we will use.


In assignment two we used character arrays (char [#]) for the input of strings (collections of characters such as a person's name). Again, this is valid C++ code, but it is not the accepted AP style. The AP style includes its own special string definitions. These definitions are incorporated into our programs using another header file (like iostream.h which we already use). This file is called apstring.h, and we include it in our programs in the same way which we include iostream.h, that is

#include <apstring.h>

This file, however, is not already installed. We must install it in C++ ourselves. Before we do this, it is helpful to understand what we are installing. A header file (such as apstring.h) is just a bunch of function definitions that someone else has already written. The functions themselves are stored in another file called a library file. If you want to use these functions you need to tell C++ that you are going to use the functions by including the header file. You also need to tell it where to find the functions themselves (i.e. where the library is). This process is usually done when you will be using a function a lot. For example, if you were going to use the pizza function from the previous assignment a lot, you could construct a library that contains this file, and make a header file for it that you could include in your program. This would save time when you wanted to use this function, you could just #include the file rather than entering the whole function again. This also allows you to share your functions with other people by giving them your library and header files.

Ok, so now we have to install the apstring information into our C++. The first thing you need to do is find the files that I have stored on the J drive. Go to J:\users\cl00\labuser\APClasses. You need to copy this whole directory to the main directory of your H drive. (If you are doing this from home, you can download a zipped version of the APClasses, and place them in the main directory of your C drive.  You can then substitute C for H below.)  You should now have a directory called APClasses in your H drive. Now follow the following steps:

  1. Start the VC++ environment.
  2. Select Tools, Options, Directories
  3. Under the "Show Directories for" window select Include files
  4. At the bottom of the list type H:\APClasses
  5. Click and drag that path to the top of the list.
  6. Under the "Show Directories for" window select Library files and follow steps 4 and 5
  7. Under the "Show Directories for" window select Source files and follow steps 4 and 5

You are now ready to try a sample program using the apstring.h file. Simply include the apstring.h at the top of your file. You do not need to include iostream.h if you do this, because apstring.h already does this for you. You then can declare strings as type apstring instead of char[]. There are some useful functions in associated with apstrings which I demonstrate here. We will discuss them shortly. Here is a small program that uses some of them:

#include <apstring.h>
void main(void)
    apstring name;
    cout<<name+" Klopfer"<<endl;

See if you can figure out what these statements do. Modify them to use your own name. See if you can enter a name using cin with the type apstring.

Back to Home Page House3.wmf (25540 bytes)