CS 1440 Lab 9

Cover sheetPrevious ActivityNext Activity


Activity 9-1:

Activity Goals

Activity Procedure

Getting Ready

Please open a telnet session on the cs machine and log on. Then create a new lab directory and change to it:
cd 1440
mkdir lab9
cd lab9

We'll put some programs in this directory a little later.

You have had lots of opportunities to use conditional and loop structures (e.g. if..else, while) by now. This lab is partly a review of previous techniques and an introduction of a few alternatives. We'll also work with functions which return logical values as a way of cleaning up our logic.

Logical Expressions

Let's review logical expressions first. In C++ when a logical value is expected, 0 means false and anything else means true. You'll see when we cover strings that this interpretation will make our life easier. But this liberal interpretation of true/false can lead to logic errors. For example, what does the following code do?
int x=5;
if(x=6) cout  << "Equal!";
else    cout  << "Not equal!";

Surprise! The output is

Equal!

We meant to use the relational expression x = = 6 rather than x = 6. The problem is that the assignment operator "=" returns a value. The value of x = 6 is the value of the left side after the assignment is performed, i.e., 6. Is 6 true or false? Since it's not zero, it is taken as true.

While any nonzero value can be used for true, when a logical expression is calculated as true, its value is 1. For example, the value of the expression 2 > 0 is 1. Sometimes it is important to know this; for example, given the integer variable x, the code

if(1 < x < 10)cout << "x is between 1 and 10";
else cout << "x is not between 1 and 10";

will always print

x is between 1 and 10

Can you figure out why? What should you have said instead of 1 < x < 10?

Let's brush up on evaluating logical expressions. This applet will let you practice for as long as you can stand it.

Functions that Return a Logical Value

Functions can be used to return a logical value. Such functions are declared as bool and return true (1) or false (0) For example:
bool IsUpper(char c)
{
        if(c>='A' && c<='Z')   return true;
        else                   return false;
}

This function returns true if c is an uppercase character, and false otherwise. A sample use:

char x;
cin >> x;
if(IsUpper(x))  cout << "Uppercase";
else            cout << "Not uppercase";

We could actually shorten our function a little:

bool IsUpper(char c)
{
        return c>='A' && c<='Z';
}

Actually, we don't need to write this function at all; there's a function isupper declared in cctype. If you add

#include <cctype>

to the top of your program, you can use isupper and several other character functions (listed on page 266 of our text).

Activity Followup

Write a function IsDigit which returns true if its character argument is a digit (0, 1, 2, 3, ..., 9) and false otherwise. There's a pre-defined function isdigit in cctype that is designed for this purpose, but please write your own. Write a program digit.C to test this function. The program asks the user to enter a character and uses the IsDigit function to output whether or not the character is a digit. NOTE: please do not use the isdigit pre-defined function in the function IsDigit. You have to figure out a way to find whether a character is a digit.