Overview
ASSIGNMENT OPERATORS
On the face of it, in many ways, C++ seems to favour lazy typists as there are numerous short-cuts designed to save typing.
Among the most common are the assignment operators :
int num = 123; // assigns the value 123 to the variable num num = num + 10; // adds 10 to num
The above can be written:
num += 10; // adds to to num
Here is a list of these shortcuts:
num += 10; // same as num = num + 10 num -= 10; // same as num = num - 10 num *= 2; // same as num = num * 2; num /= 4; // same as num = num / 4; num %= 3; // same as num = num % 3
The % is the modulus or remainder operator, so the following :
int num = 10; num %= 3;
would result in 10 being divided by 3 and the remainder 1 would be stored in num.
EXERCISE 5
- Using the += assignment operator write a program that will list the eight times table to the screen.
- Add the -= operator to the program so that after listing the last value, it then prints the table backwards.
- Add the *= operator to the program so that it prints the powers of two to the screen i.e. 2, 4, 8, 16 etc.
- Add the /= operator so that after printing the last power of two the list is printed backwards.
- Add the modulus operator to the program in such as way as to list all the table results of the first table that will divide by 3 without leaving a remainder.
DATA VALIDATION
When dealing with "user input" in a program, there is a need to check the user's response for validity. If this is not done, an invalid response could crash the entire system. Thankfully the following libraries come to the rescue with the following macros :
MACRO Tests for
isalpha() Alphabetic character
isupper() Uppercase letter
islower() Lowercase letter
isdigit() Decimal digit
isalnum() Alphanumeric character
isspace() Whitespace character i.e. Space, form
feed (\f),(\n), carriage return (\r),
horizontal tab (\t) and vertical tab (\v)
ispunct() Punctuation character
iscntrl() Control character
isascii() An ASCII value i.e. 0 - 127
Since when has an integer been 2 bytes? According to the C and C++ standards,
an int can be any value larger than a char. It can be
101 bytes long if the platform requires it to be!
It also shows the age of the original code - MS-DOS C was a 16 bit compiler, so
it was common for authors to (incorrectly sometimes) define an int as
being 2 bytes. Again, if the code had been checked on a newer compiler, the
result would not have been 2 bytes
All of the above macros take an integer (2 bytes) as an argument and returns an integer. This is because some keys (e.g. Function keys) produce an integer value. The macros can however still be used with char values (1 byte).
EXERCISE 6
While not a bad exercise, it does again show the C basis again. The C++ version would use something more like
while (cin >> Answer, Answer < 0 || Answer >9)
{
cout << "Invalid entry" << endl;
cin.clear();
cin.ignore();
}
which is a damned sight more beneficial to the student than using the C methods outright. Hmm, what's a function key?
Write a program that asks the user to press a number key. If a letter key is pressed, print an "Access denied" message and loop again, otherwise, print a thank you message, wait for a keypress and exit gracefully. Notice what happens when the user presses a function key. Try to work out why. Is there a way to make the program work with function keys?
Hint:
cin ignores whitespace and will wait for a carriage return, so is not suitable for this type of progam. Try experimenting with the getch() function prototyped in conio.h. More on prototyping later.
What on EARTH are they doing using a platform specific library in a generic
language course? conio.h is the evil bastard spawn which should be
avoided at all costs when dealing with students
EXERCISE 7
Modify the previous program to work with letter keys.
FUNCTION PROTOTYPES
When the ANSI C standard was implemented from C, it was the C functions that underwent the greatest change. The ANSI C standard for functions is based upon the function prototype that has already been used extensively in C++.
This paragraph is complete crap. You don't need functions to be prototyped. Okay, I'll explain that a bit more. If a single source file, you should never need to prototype functions. In a multi-source file situation, you may have to prototype some functions, but certainly not all and that is only so that the compiler doesn't cough, splutter and moan when it tries to compile code with functions defined outside of the current source
Under the ANSI C standard, all functions must be prototyped. The prototyping can take place in the C or C++ program itself or in a header file. The function prototype is simple, and is usually included at the start of the program code to notify the compiler of the type and number of arguments that a function will use. Prototyping enforces stronger type checking than was previously possible when the C standards were less strongly enforced.
Assume that a function called sqr has to be written. The criteria for the function is to be
This is a poor example as the cmath header already has a function
called sqr
- The function is to take a copy of an integer as an argument and print the square of the number to the screen.
- The original integer is not to be changed in anyway, and a return value from the function is not needed.
First the prototype needs to be coded. The prototype can take one of two forms:
void sqr(int); void sqr(int number);
Notice the difference? Either of the above will work, but the second is preferred, as it is more descriptive and will be used for the remainder of this booklet.
The return type comes first in the prototype. In this case it is void, which means there is no return value.
What? The function is void (return type) sqr (the
function name) (int number); (the arguments being passed in)
Next comes the function name, followed by it's argument type and name.
Finally, a semi-colon must be added. The semi-colon is crucial, as it is the only way the compiler can tell the difference between the function prototype and the start of the function.
None of these are macros - they're functions held in
cctype. You have to question the author's knowledge to get something as fundamental as a macro and function incorrect. While a macro can be used as a function, they are not the same!