Topic 2. Dissecting a C++ program Learning Objective. At the end of this lesson, students will be able to : 1. 2. 3. 4. 5. Describe the basic elements in a C++ program Define various data types Declare and use variables in programs Use arithmetic operators in statements Use input and output statements Glossary : Syntax Set of rules one must follow when using a programming language Token A sequence of characters separated by delimiting characters (usually spaces) Identifier Names of things that appear in programs Data type A set of values together with a set of operations Attention grabber. Standunder me talk you no yes! (Do you understand what I am saying?) 1.0 Basic Stucture of a C++ Program You now know that a program must be written using a programming language. In order to write a program using a programming language, you must learn to understand that language. Consider what you need to learn in order to understand and write in a foreign language. You will need to learn the alphabets, then using the alphabets you learn to write words, using the words you learn how to write sentences, and using the sentences you learn how to write an essay. Similarly, to be able to write a program, you need to learn the symbols that make up the words which you can use in your programming statements. When you write in English, or Arabic , or in any language, you must know the grammar for the language. That is the rules that you need to follow. Similarly with programming languages, you must also know certain rules and this rules are called syntax rule. In this chapter you will learn about the syntax of the C++ language. A C++ program is made up of one or more functions. As we have seen in the earlier chapter, main() is one of the functions you will encounter most in a C++ program. If your program has only one function, then main() function is the one it must have. This is an example of the syntax rule which you must follow. If you don’t have this main() function, the computer will not understand your program and will give you an error. The syntax of the main() function is in the following form : int main() int main() { { Statement 1; statement 1; . . . . . . Statement n; statement n; return 0; } return 0; } This main() function is called a pre-defined or standard function. There are other such functions, and these pre-defined or standard functions are provided to you as part of the C++ development system. But you will later learn to write your own functions to do certain tasks which you require the program to do for you. We will learn more about writing your own functions later. Before we do that, we need to look at in more detail the other elements in a program. As you have learned in chapter 1, there are various elements that make up a C++ program. The ones you have seen are comment, preprocessor directive, function, body of the function, and statements. In addition to that, you need to understand the symbols that can be used, the words or the identifiers allowed in a program and a special element called whitespaces. 1.1 Comments As a programmer, you will write not one, but many programs. You will not be able to remember the details of these programs in your head, therefore, you need to keep certain notes or remarks to remind you of various things about the program. You can use comment to do this. Comments can be used to indicate the date the program was written or modified, give a short explanation about the program, explain the meaning of key statements, and other relevant matters. Comments are meant to help the programmer or anyone who reads the program codes. They are not meant for the computer, and when the computer executes the program, it will disregard the comments. To tell the computer which lines are meant to be comments, you need to use the /* and */ symbols. The /* symbol indicates the beginning of a comment while the */ symbol indicates the end of the comment line. This is another example of syntax. If you use any other symbols, the computer will not recognize them and you will be given an error. This type of error is called syntax error. However, besides the symbols given above, the // symbol can also be used for comments. This symbol can be used within the same line as a statement. The following are examples of valid comments. /* This is one way to write comment */ // This way is also correct to write a comment. No need closing symbol // You can add another line of comment here cout << “HELLO”; // This statements prints the word HELLO /* If you have more than one line of comments, you can continue to do it like this and close at the end of the comment statement. */ 1.2 Special Symbols A program statement is made up of various elements, and the smallest element is called a token. In C++, tokens can be special symbols, reserved words, and identifiers. Below are special symbols used in C++ with it’s meaning. Each of the symbols is used in mathematical statements. + is for addition + - * / - is for subtraction * is for multiplication / is for division , is used to separate items . ; ? , ; is used at the end of a statement . and ? are for special things which will be discussed later Each token is made of of two symbols. Each symbol is typed right after the other, with no spaces between them. <= != == >= <= means “less or equals to” != means “not equals to” == means “equals to” >= means “greater or equals to” Whitespaces includes blanks, tabs, and newline characters. Whitespaces They are used to separate tokens in a statement. When (blank space) printed, they look like blank spaces. They are used to make your program readable. 1.3 Identifiers Identifiers are names of things in a program. Things in programs are variables, constants, and functions. Some identifiers are pre-defined, and some you will name them according to your needs. If you are naming your own identifier, you need to remember that when you name things (identifier), you must follow certain rules as follows : Identifier consists of letters, digits and underscore character (_) Identifier cannot begin with a digit Identifier cannot have any symbols except underscore. The following are legal identifiers : height counter unit myWeight addressLine1 The identifiers the following table are illegal Illegal Identifier Reason staff name Cannot use space (blank) between Staff and Name house# Cannot use symbol 1stwinner Cannot start with digit income-expenses Cannot use symbol Note : because C++ is case sensitive, the identifier height and Height are not the same to the computer. Identifier myWeight and myweight are not the same either. Also, it is not advisable for you to start your identifier with underscore(_) because this is usually the way the system name its identifier. 1.4 Reserved words Although you can use any words or any combination of letters to be identifiers, there are some words which have already been reserved for the system to mean certain things and you can’t use them to name your variables, constants or functions. These reserved words are also referred to as keywords. These are some of the reserve words : int, float, if, do, char, void, return, then, const and many more. You will learn to recognize more of these words as you gain more experience in the language. Next, we are going to look at more elements that make up a C++ program. 2.0 Data types The basic elements we looked at earlier are only the beginning of our journey. If you remember the currency converter program that we have written in chapter 1, we have written many lines of statements and now it is time for us to try and understand the codes. The objective of the currency converter program is to manipulate the data given by the user and calculate it, and produce an output as required by the user. In this particular program, you need to deal with a mathematical equation, which is RM = USD x rate To perform this multiplication, you require data that are numbers. You cannot multiply data that are alphabets. And you need to clarify the type of the data you are using to the computer. This can be done by specifying the data type. In C++ language, data types are made up of simple data type, structured data type, and pointers. In this section, we are going to explore only the simple data type. We will look at the other two data types in later chapters. 2.1 Simple Data Types Simple data types are the fundamental data types, and they can be used later to define structured data types. There are basically three categories of fundamental data type : Integral deals with integer numbers, and characters Floating-point deals with decimal numbers Enumeration deals with user defined data type (which will be discussed in a later chapter) 2.1.1 Integral data type There are many types of integral data. The ones that you will use mainly are int, char and bool. Data type int is used to represent integer numbers. Example of integer numbers in C++ are -6473, -54, 0, 463, 8372 and so on. You must use the ‘-‘ sign to indicate a negative integer number, but you don’t have to use the ‘+’ sign to indicate a positive integer number. However, it is not en error to do so. Therefore, both 764 and +764 are considered the same to the C++ language. You must also remember that you cannot use ‘,’ to indicate an integer number. The number 1,346 will give you an error, but the number 1346 is valid. The next data type is char. It is used to represent characters – such as letters (alphabets), digits, and special symbols. When using the char data type, you must enclose each character in single quotation marks. Examples of characters are ‘A’, ‘a’, ‘8’, ‘+’, ‘%’, ‘$’ and the space character which is shown as ‘ ‘, with a space in between the single quotes. There is only one symbol for each character. Therefore, ‘etc’ is not a valid char data type. For data which is made up more that one character such as a name of a person or a word, there is a special data type called string, which we will look at in a later section. The char data type contains values for each of the character on the keyboard. There are several different character dataset being used, and the most common is the ASCII character set. ASCII is the abbreviation for American Standard Code for Information Interchange. It has 128 values. For example, in the ASCII code, the character ‘a’ is represented by the value 97 and the character ‘A’ is represented by value 65. You can find the full set of ASCII character set on internet. Figure 1. ASCII characters The next data type is the bool data type. It is used to represent logical Boolean values, which are the values true and false. We will discuss this more in later sections. 2.1.2 Floating-point data type Numbers are not only restricted to integers. In many cases, we have to make use of decimal numbers such as 10.2, or 76.362 and so on. To deal with decimal numbers, you have to use the floating-point data type. There are three possible floating-point data types; float, double, and long double. In section 4.0, we will see why this is so. 2.2 String The string data type is not a simple data type. It is actually a data type that has been defined by the programmer of the C++ program. This data type has been defined and stored the C++ program library. So, to use this data type, all you need to do is to access the string components from the C++ library. To do that, you need to include the following pre-processor directive : #include <string> We will see examples of this later. Now that we know the different data types that we can use in C++ program, how do we use them? Remember the program we coded, compiled and executed in chapter 1? Let’s look at the program again. In order to make it easier to refer to the program statements, I will place the line number for each of the line in the program codes. 1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5. float usdAmount, cRate, rmAmount; 6. 7. cout << "Amount of USD to convert : "; 8. cin >> usdAmount; 9. 10. cout << "Enter conversion rate : "; 11. cin >> cRate; 12. 13. rmAmount = usdAmount * cRate; 14. cout << "USD" << usdAmount << " is equivalent to RM" << rmAmount << endl; 15. system("pause"); //this statement is needed when using DEV C++ 16. 17. return 0; 18. } In this program, the value of USD and conversion rate must be entered. Therefore, the program must have a way to handle these data. To do this, the program needs to store the data even if temporarily, and it has to know the type of the data. This is done through the use of variables. 3.0 Variables Variables is a place where a program can temporarily store or “remember” data that are being used in the program. Variables are actually locations in the computer’s memory. Using the example of the program above, in order to convert the USD to RM, the program must hold the value of the USD and the conversion rate, and later, the value of RM after the calculation has been completed. Such, these values need to be stored in three different variables. In order to know which variable the code is referring to, the variables must be given names. If you refer back to section 1.3, variable names are called identifiers. And you must follow the rules in order to give the names to your variables. Following those rules, in the currency conversion program above, we have named the three variables as usdAmount, cRate, and rmAmount. These are valid identifiers and they also give meaning to the values in the variables, which make the source codes easier to read and understand. To use the variables, you must first declare them in the program. When you declare a variable, you are telling the program what you are going to store in the variable; what type of data are you going to store at that location. Line 5 shows the declaration statement. To declare a variable, you need to specify the data type followed by the name of the variable. You can declare more than one variables of the same data type in one statement as seen in line 5 of the program above. Other examples of declaration are as follows: int age; char gender; string staff_name; 3.1 Variables and memory location In section 3.0, it was mentioned that variables are actually locations in the computer’s memory. What does this mean? When you declare a variable, the program will find a location in the computer’s memory (RAM) and temporarily assigns a variable name to that memory location. Value which is associated with that variable will be temporarily stored in that location. The value at this location will change whenever a new value is associated with the same variable. When a new value is placed in the memory location, the previous value is destroyed. The new value overwrites the old values. When the program wants to reserve a memory location for a particular variable, it needs to know how much space (bytes) to allocate for that variable. And the program can get that information by knowing the data type of the variable. For example, if the variable is for data type integer (int), then the program will allocate 4 bytes for it. If the variable is for data type character (char), then the program will allocate 1 byte for it. The following table lists some of the information relating to some of the more popular data types in C++. Data type Stores Memory Required Values char One character 1 byte One character eg. A short integer 2 bytes -32,768 to 32,767 int integer 4 bytes -2,147,483,648 to 2,147,483,647 float Single precision floating-point number 4 bytes -3.4e38 to 3.4e38 (7 digits of precision) double Double precision string 8 bytes floating-point (15 digits of number precision) Zero or more 1 byte per character zero or more characters bool -1.7e308 to 1.7e308 Boolean value characters 1 byte TRUE, FALSE 3.2 Assigning value to variables Once you have declared a variable, you can use it to store a value. Value can be assigned to variables through the “cin” statement, such as the one used in the currency conversion program lines 8 and 11 : cin >> usdAmount; cin >> cRate; and This statement is also known as the read statement. The cin statement uses the input stream objects from the iostream file, obtains the value keyed in from the keyboard and “gives” the value to the variable specified after the “>>” symbol. In line 8, the value keyed in from the keyboard is given to the variable named usdAmount, and in line 11, the value keyed in from the keyboard is given to the variable named cRate. The process of “giving” a value to a variable is called “assignment”. Therefore, you can say that in the above program, the variables usdAmount and cRate are assigned values from the keyboard. There is another way how variables can be assigned a value, which is using a statement called the assignment statement. An assignment statement uses the symbol “=”. Symbols such as this one are called “operator”. We will take a look at operators in the following section. The following are some examples of how assignment statements look like. usdAmount = 45.50; year = 1998; gender = ‘F’; answer = usdAmount; In an assignment statement, the variable name is placed on the left side, and the value to be assigned to the variable on the right side of the “=” symbol. Take note that you can assign a value in a variable to another variable as shown in the last example above. There is another way to assign a value to a variable, and this is called variable initialization. This can be done when you declare the variable, and at the same time, assign a value to it. The following are some examples : float usdAmount = 45.50; int year = 1998; 3.3 Variables changes in values To further illustrate what happen to the values of variables, let us take a look at the following statements in a program. 1. int No1; 2. cout << “Please enter an integer number : “; 3. cin >> No1; 4. cout << “The number stored in variable No1 is “ << No1; 5. No1 = 15; 6. cout << “The number stored in variable No1 now is “ << No1; 7. No1 = 1; 8. cout << “The number stored in variable No1 now is “ << No1; The statement in line 1 declares the variable. Remember that a variable is a location in the computer’s memory. Let us visualize this memory location as a box that can hold a value. The box I s visualized as follows: The question mark in the box indicates that at this point of time ? there is no value assigned to the variable. No1 The table below shows the changes in the value of the variable No1 after each statement is executed. Program Statement Display on monitor int No1; cout << “Please enter an integer Value in No1 ? Please enter an integer number : ? number : “; User will respond by typing the number 45 using the keyboard cin >> No1; cout << “The number stored in variable 45 The number stored in variable No1 is 45 No1 is “ << No1 << endl; 45 No1 = 15; 15 cout << “The number stored in variable The number stored in variable No1 is 15 15 No1 now is “ << No1 << endl; No1 = 1; cout << “The number stored in variable No1 now is “ << No1; 1 The number stored in variable No1 is 1 1 3.4 Constants A variable is used whenever you want the program to store a temporary data which may change in value during various stages of the program execution. However, you may want the program to store a permanent set of values which you may want to identify or not. A value which remains the same throughout the execution of the program is called a constant. For example, in the conversion program shown earlier in this chapter, if the conversion rate is fixed, then you can choose to include it in the program as a constant. Please see the modified version of the same program beow: 1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5. float usdAmountrmAmount; 6. const float C_RATE = 3.5; 7. 8. cout << "Amount of USD to convert : "; 9. cin >> usdAmount; 10. 11. 12. rmAmount = usdAmount * C_RATE; 13. cout << "USD" << usdAmount << " is equivalent to RM" << rmAmount << endl; 14. system("pause"); //this statement is needed when using DEV C++ 15. 16. return 0; 17. } Take note that line 6 declares a constant of the name C_RATE, and assigns it a value of 3.5. The word “const” which precedes the declaration statement is called a modifier. This marks the C_RATE as a constant and does not allow the program to change its value. A constant is usually named in all capital letters. This is not required by C++, but this is the standard practice used by C++ programmers. 4.0 Arithmetic Operators The currency conversion program must perform a calculation to convert US dollars into RM by multiplying the US dollars by the conversion rate. As a matter of fact, most programs require the computer to perform some kind of calculations. To instruct the computer to perform these calculations, you need to write an arithmetic statement using arithmetic operators. Table below lists the arithmetic operators you can use in C++, in order of their precedence. Operators () Operations Parentheses * / % + - Multiplication Division Modulus Addition Subtraction Precedence Evaluated first. If the parentheses are nested, the innermost pair is evaluated first. These operators are evaluated second. If there are several of them in the same statement, they are evaluated left to right. Evaluated last. If there are several of them in the same statement, they are evaluated left to right. What that means is that, when there is parentheses in the arithmetic statement, then, the expression within the parentheses will be evaluated first, followed by multiplication or division, or modulus, depending on which one is situated at the leftmost position of the statement, followed by addition or subtraction, depending on which one is situated at the leftmost position of the statement. See the following examples. Examples : 5 + 12/3 -1 12 is divided by 3, then 5 is added to the result, and then 1 is subtracted from that. The result of this statement is 8 5+12 / (3-1) 3 minus 1, and then 12 is divided by that result, and lastly 5 is added. The result of this statement is 8. While the arithmetic operations are similar to the algebra operations you may have learned in school, the modulus (%) operation may be new to you. The modulus operator is used to divide two integer numbers, and results in the remainder of the division. For example, the statement 11%2 results in 1,which is the remainder of 11/2. One of the use of modulus operation is to determine if an integer number is an even or an odd number. If you divide a number by 2 and the remainder is 0, then you know it is an even number. If the remainder is 1, then you know it is an odd number. Such, you can use the arithmetic statement 45%2 and if the result is 1, it will tell the computer that 45 is an odd number. 4.1 Combining arithmetic operators in assignment statements Consider the following statement : usdAmount * cRate; When the system executes this statement, it will take the value in the variable called usdAmount and multiplies it with the value in the variable called cRate. The system will have the result of that multiplication, but it will not store that result anywhere in its memory because, if you remember, in order for the system to store a value in it’s memory, you need to use the assignment statement. Therefore, in many cases, you will see that arithmetic operators and operations are usually combined in an assignment statement as shown below (which is the statement we used in line 13 of the currency conversion program) rmAmount = usdAmount * cRate; Other examples are as follows : sum = sum +total; kg = lbs * 2.2; results = 5+12 / (3-1); 5.0 Input and Output Statements 5.1 Using cin operator You have seen the read or the input statement being used in the currency conversion program. The input statement which uses the cin and the >> operators from the iostream file, obtains the value keyed in from the keyboard and assigns the value to the variable specified after the “>>” symbol. The syntax of the input statement is cin >> variable >> variable …….. ; Which means that you can read more than one value using one input statement. Suppose we have declared the following variables : int feet, inches; You can then read both input from the same statement if you have a statement like this: cin >> feet >> inches; When you have an input statement as given above, the first data from the keyboard will be assigned to feet and the next will be assigned to inches. A sample program on how this is done is given below : //Using a single input statement for two inputs #include <iostream> using namespace std; int main() { int feet, inches; cout << “Enter two integers separated by spaces : ”; cin >> feet >> inches; cout << endl; cout << “Feet has the value : “ << feet << endl; cout << “ Inches has the value : “ << inches << endl; return 0; } What would you get if you run the program? Below is the display and output you will see on your monitor. (Note: since the program requires input from the keyboard, we will provide the user input, which is shown as shaded.) Enter two integers separated by spaces : 2 7 Feet has the value : 2 Inches has the value : 7 5.2 Using cout operator You have been using the output statement to display output on the computer screen. The output statement which uses the cout and the << operators from the iostream file, specified after the “<<” symbol. The syntax of the input statement is cout << expression or manipulator << expression or manipulator …….. ; Which means that you can output more than one value using one output statement. The following are samples of output statements and the output which will be displayed as a result of that statement. Cout << 29/4 ; 7 Cout << “Hello there”; Hello there Cout << 12; Cout << “29 / 4”; 12 29 / 4 Cout << ‘A’; A Cout << 2+3 * 5; 17 Cout << “4 +7 = ” << 4 + 7; 4 + 7 = 11 Cout << “Hello\n” << “there”; Hello there The expression 29/4 is evaluated, and the result is displayed within the “ “ A string is displayed exactly as it is A constant is displayed Whenever you use “ “, everything within the quotes are considered a string and will be displayed as it is A character is displayed. A character uses single quotes ‘ ‘ The expression is evauated and the result is displayed There is values to be displayed. The first is the string within the quotes, and the second is the result of the expression. The \n is called a newline character. When \n is encountered within a string, the following value will move to the next line. What happens is that the insertion point (the cursor on the screen) is Cout << “Enter a number : “; Enter a number : Cout << “Line 1 “ ; Cout << “Line 2 “; Line 1 Line 2 Cout << “Line 1 “ << endl; Cout << “Line 2 “; Cout << “Line 1 \n“ ; Cout << “Line 2 “; Line 1 Line 2 Line 1 Line 2 positioned at the beginning of the next line. This is a sample of how cout can be used to instruct (prompt) the user on what to do. This is also called a prompt line. The output of these two couts are displayed on the same line next to each other because there is no line breaks. Line breaks can be implemented using the \n newline character or the endl stream manipulator. Using the endl manipulator, the system will output a newline. The output looks the same. The only difference is that the \n newline character must be inserted within a string “ “. 5.3 Using escape sequences in output statements Recall the \n newline character in the above examples. This special character preceeded with a backslash is called an escape sequence. There are other escape sequences you can use to help you in displaying your output. There are other escape sequences which you may want to use in your programs. Examples are shown in the table below. \n Newline \t \r Tab Return \\ \’ \” Backslash Single quote Double quote Cursor moves to the beginning of the next line Cursor moves to the next tab stop Cursor moves to the beginning of the current line (not the next line) Backslash is printed Single quotation mark is printed Double quotation mark is printed 5.4 Formatting floating point numbers When a decimal number is displayed in C++ program, it is displayed either in fixed-point notation (5464.73) or exponential notation( 5.46473e+3). Actually, numbers containing six or fewer digits to the left of the decimal point are displayed in fixed point notation, while numbers containing more than six digits to the left of the decimal points are displayed in e notation. However, as a programmer you can determine how you want your output to be displayed. If you want to control the format of the output of the floating-point number in your program, you can use stream manipulators. The fixed stream manipulator is to ensure that the floating point number is displayed in fixed point notation, while the scientific stream manipulator for e notation. However, in order to use these manipulators the program must contain the #include <iostream> directive, and the using std::fixed; statement for fixed notation, and the using std::scientific; for e notation. The program below illustrates. #include <iostream> using namespace std; using std::fixed; using std::scientific; int main() { double sales = 10575.25; cout << fixed << sales; cout << endl; cout << scientific << sales; system("pause"); return 0; } When the program is executed, this is the output : 10575.2500 1.057525e+004 But what if you want to control the number of decimal points in the fixed notation? What if you want the output to display 10575.25 (in two decimal points)? You can use the C++ setprecision stream manipulator. To use this manipulator, the program must contain the #include <iomanip> directive and the using std::setprecision; statement. See the following example : #include <iostream> #include<iomanip> using namespace std; using std::fixed; using std::scientific; using std::setprecision; int main() { double sales = 10575.25; cout << fixed << setprecision(2) << sales; cout << endl; cout << scientific << sales; system("pause"); return 0; } When the program is executed, this is the output : 10575.25 1.057525e+004 6.0 Exercises