Essential Math Operators


I know a lot of people don't like math very much, but you have to be able to do some math to write games. Fortunately, many types of games don't require heavy-duty math. In fact, you can do a large amount of programming with nothing more than the basic math operators: addition, subtraction, multiplication, and division.

Note

You may have noticed that each line of code in the listings in this book is rather short. Because of the limitations of the printed page, I can have only about 70 characters per line of code (including spaces). In your programs, you have no such limitations, so you can make each line of code as long as you want it. However, most programmers keep their code to 80 characters per line to make them more readable.


Addition, Subtraction, Multiplication, and Division

The symbols you use for addition and subtraction in C++ programs are the symbols you'd expect, + and -. For division, you use the slash (/). Multiplication uses the asterisk (*) rather than an x. Let's do a quick program (listing 2.4) to demonstrate.

Listing 2.4. A short demonstration of +, -, * and /

 1    #include <iostream> 2    #include <stdlib.h> 3 4    using namespace std; 5 6    int main(int argc, char *argv[]) 7    { 8        int anInt, anotherInt; 9 10       // Prompt the user for an integer. 11       cout << "Please type an integer and then press Enter: "; 12       cin >> anInt; 13 14       cout << "Please type another integer and then press Enter: "; 15       cin >> anotherInt; 16 17       cout << anInt << " + " << anotherInt << " = "; 18       cout << anInt + anotherInt << endl; 19       cout << anInt << " - " << anotherInt << " = "; 20       cout << anInt - anotherInt << endl; 21       cout << anInt << " * " << anotherInt << " = "; 22       cout << anInt * anotherInt << endl; 23       cout << anInt << " / " << anotherInt << " = "; 24       cout << anInt / anotherInt << endl; 25 26       float aFloat, anotherFloat; 27       cout << "Please enter a floating-point number"; 28       cout << " and then press Enter: "; 29       cin >> aFloat; 30 31       cout << "Please type another floating-point number"; 32       cout << " and then press Enter: "; 33       cin >> anotherFloat; 34 35       cout << aFloat << " + " << anotherFloat << " = "; 36       cout << aFloat + anotherFloat << endl; 37       cout << aFloat << " - " << anotherFloat << " = "; 38       cout << aFloat - anotherFloat << endl; 39       cout << aFloat << " * " << anotherFloat << " = "; 40       cout << aFloat * anotherFloat << endl; 41       cout << aFloat << " / " << anotherFloat << " = "; 42       cout << aFloat / anotherFloat << endl; 43 44       system("PAUSE"); 45       return 0; 46  } 

The output from the program in Listing 2.4 is shown in Figure 2.5.

Figure 2.5. The output from Prog02_04.


The program in Listing 2.4 begins by declaring two integer variables on line 8. Next, it prompts the user to type in an integer and reads in the response. The program then asks for another integer and reads it into the program on lines 1415.

Note

You can declare more than one variable on a line of code by separating the variables with commas. All of the variables declared on one line of code are the same type.


On line 17, the program prints the first integer to the screen. As in the previous example programs, it sends output to the screen using the cout stream and the insertion operator. Notice that you can use the insertion operator more than once on a line of code. On line 17, the insertion operator appears four times. The result is that line 17 first sends the value in the variable anInt to the screen. It then sends a string containing a space, a plus sign, and another space, in that order. Next, the program prints the second integer the user typed in, followed by a space, an equal sign, and another space. Line 18 prints the result of adding the two integers. Finally, it prints an endline.

The result of lines 1718 is shown in Figure 2.5. When prompted, I typed in the numbers 2 and 3 for the two integers. Lines 1718 used them to print the following output:

2 + 3 = 5

Notice that even though it took two lines of code to print the output, the result contains only one line of output. That is because the program prints all output on the same line until it encounters the endline symbol.

Lines 1920 print the result of subtracting the two integers. Notice from the output shown in Figure 2.5 that the result in this case is negative, which is what you'd expect. Lines 2122 print the result of multiplying the two integers. The result in this case is also what you'd expect: the number 6. Lines 2324 print the result of dividing the two integers. This result is not what most people expect. The output in Figure 2.5 shows that the result of dividing 2 by 3 is 0. How did that happen?

When you use integers in programs, they cannot have any decimal points. Put another way, integers have no fractional parts. They are whole numbers only. When you divide 2 by 3, the real answer is the fraction 2/3. Using a decimal point, that comes out to 0.6666666 (the 6 repeats infinitely). However, the program is using integers here because the variables anInt and anotherInt are declared as type int on line 8. As a result, the answer must also be an integer. Although the real answer is 0.6666666, the program forces the answer to be an integer by throwing away the decimal point and everything to the right of it. Only the 0 is left.

Starting on line 27 of Listing 2.4, the program uses floating-point numbers. Floating-point numbers have fractional parts, so they can use decimal points. The program demonstrates adding, subtracting, multiplying, and dividing floating-point numbers. Notice that the division has no problem with the fact that the answer has a fractional part in it.

Tip

If the answer to your calculations is going to be a number with a fractional part, be sure to use floating-point numbers (type float or double) rather than integers.


Increment and Decrement Operators

In addition to the four standard math operators, C++ provides operators to add or subtract one to the value in a variable. Adding one to a variable is called incrementing it. Subtracting one is called decrementing it. Incrementing and decrementing are tasks that your programs need to perform a lot, so it's important to learn them.

The increment operator is two plus signs with no space between them (++). Likewise, the decrement operator is a pair of minus signs (--). Listing 2.5 shows how to use them.

Listing 2.5. Using the increment and decrement operators

 1    #include <iostream> 2    #include <stdlib.h> 3 4    using namespace std; 5 6    int main(int argc, char *argv[]) 7    { 8      // Declare an integer variable. 9      int anInt; 10 11     // Prompt the user for an integer. 12     cout << "Please input an integer and press Enter: "; 13     cin >> anInt; 14 15     // Increment the integer and show the result. 16     anInt++; 17     cout << "After increment: " << anInt << endl; 18 19     // Decrement the integer and show the result. 20     anInt--; 21     cout << "After decrement 1: " << anInt << endl; 22 23     // Decrement the integer and show the result. 24     anInt--; 25     cout << "After decrement 2: " << anInt << endl; 26 27     system("PAUSE"); 28     return 0; 29  } 

You can see the output from the program in Listing 2.5 in Figure 2.6

Figure 2.6. The results of using the increment and decrement operators.


The program in Listing 2.5 starts by declaring an integer variable. It prompts the user to input an integer. Whatever value the user enters, the program increments it on line 16. It outputs the result on line 17. On line 20, it decrements the value in the variable anInt, outputs the result, decrements it again, and outputs the result one final time. Line 27 causes the program to pause until the user presses a key on the keyboard. Line 28 returns the value 0 to the operating system to indicate that the program completed successfully.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net