views
Setting Up the Main Function

Include preprocessor directives. These are the first lines of code in the program and are preceded by a hash sign. They are needed for the program to properly compile. In this case the only preprocessor directive needed is iostream, formatted as shown below. Notice that there is no semicolon used at the end of this statement.

Use standard namespace. In addition to preprocessor directives, the first lines of the code must also define the namespace being used. The standard namespace, formatted as shown below, is sufficient for this code. Note that this line ends with a semicolon.

Define the main function. To create the main function, type “int main()” as shown below. The parentheses are for setting the parameters of the function, but here no parameters are needed and thus the parentheses are empty. There is no semicolon after the function definition.

Make curly braces immediately following the function. On the next line, make a set of curly braces as shown in the graphic. Everything included within these curly brackets is part of the main function. The code up to this point should look something like the picture below.
Writing Cout Statements

Know the syntax. Cout is used with the insertion operator, which is written as << (two “less than” signs). The actual output then follows, written within quotation marks. The line must end with a semicolon.

Write the cout statement. Within the main function, type the cout statement using the proper syntax. For example: cout << “type text here”; (or std::cout << "type text here";, if you don't like the use of using directives)

Become familiar with other uses of cout. Cout can also be used to output the values of variables, as long the variable has already been defined. Simply write the name of the variable after the insertion operator as shown below.

Use multiple insertion operators in a single statement. Insertion operators can be simply chained together, one after the other as shown in the figure.
Writing Cin Statements

Know the syntax. Cin is used with the extraction operator, which is written as >> (two “greater than” signs). The operator is then followed by a variable where the inputted data is stored. The line must end with a semicolon.

Write the cin statement. First declare a variable. Then write a cin statement to define a value for the variable as shown. When the program runs, the input that user enters will be assigned to the variable. Note that the cin statement does not output any text onto the monitor.
Combine cin and cout statements. Cin and cout statements can and should be used together. For instance, a cout statement may be used to prompt the user to assign a value to a variable, which is then assigned through a cin statement as shown in the figure.
Comments
0 comment