how to write c++ program? - pusat pengajian …ee.eng.usm.my/eeacad/junita/notes/how to write a...

12
How to Write a C++ Program? A step-by-step procedure

Upload: voxuyen

Post on 31-Mar-2018

219 views

Category:

Documents


3 download

TRANSCRIPT

How to Write a C++ Program?

A step-by-step procedure

Main Elements of C++ Program (in order)

• Declaration of appropriate header files or preprocessor directives– It tells the computer about where to look for the commands you use in

your program.

• Declaration of constants (if any)– This is not compulsory.

– Only needed if program involves constants.

• Body of program consisting of:– Main program header

– Begin main program

– variables

– C++ statements and/or subprograms, also called functions

– End main program

Step 1: Program purpose

• This is compulsory to address the objective of the program.

• The first two columns of all lines describing the program purpose must start with //

• Start your program with:// Purpose: This program calculates…

// and displays the ……

Step 2: Declare preprocessor directives

• It is compulsory to include preprocessor directives in your program.

• Preprocessor directives involves declaration of header files, which are files with the ‘.h’ extensions.

• A compulsory header file is iostream.h. The declaration is:#include <iostream.h>

This header files MUST always be used in your program. Hence, if you do not know how to start your program, just remember to start it with this line as the first line in your program.

• Other directives- Will learn as we go along.

Step 3: Declare constants (optional)

• Constants are declared after declaration of directives.

• It is optional. Only needed if constants are used in your program.

• Declaration of constants come with initialization of their values.

• E.g.: To declare pi as a constant#define pi 3.14;pi is the name given to represent the constant. The name given

to a constant can vary (up to the programmer), but make sure it is meaningful.

3.14 is the constant value.

Step 4: Begin program body

• A main program begins with the following lines:

void main()

{

• void is the program datatype. It means “nothing” will be returned. If the program will return an integer value, then the program needs to be declared as

int main()

• main() means it is a main program. This is compulsory.

Step 5a: Declare variables

• A name is needed to represent a data or value of a variable. It is called a variable name.

• Variable names must be meaningful, – E.g. height, number_1, number2, total

• Rules for variable names:– Cannot be the same as C++ keywords/commands– Cannot start with number, nor a symbol.– Cannot have special symbols within it.– If a variable name must be two words, the words cannot

be separated by a space. Use underscore (‘_’) instead.

Step 5b: Include variable datatype

• Remember? Variables represent data of different types, e.g. integer numbers, fraction numbers, text string, array

• So, must specify the type of data with variable declaration.

• Common datatypes: – Integer = int

– Floating point = float, double

– Character = char

– Condition = boolean

• E.g. A variable representing a student’s year of study: intA variable representing the result of a division: float or double

A variable representing a character: charA variable representing a condition of TRUE or FALSE: boolean

Step 5: Example of Variable declaration

Problem: To calculate the division of two integer numbers.• 3 variables are needed:

1. To store first number (let’s call it number1)2. To store second number (let’s call it number2)3. To store the result of division (let’s call it res_div)

• So, the variable declaration required is,int number1, number2;

float res_div; // why float datatype?

• Variable declaration is NEEDED BEFORE the variables are used in the program

Step 6: Write other statements

• This is then followed by other C++ statements relevant to solve the given problem.

• A completed main program is ended with a closing curly brace:

}

Step 7: Add comment lines

• Why need comments?– To help others understand the program

– To assist further modification

• How to write comments?– Comments in C++ program starts with /* and

end with */

– Or, a comment can start with // with no end of comment symbol

How does a complete C++ program look like?// Purpose: This program gets input from users and display it on the screen.

#include <iostream.h> // header file declaration #define pi 3.14; // constant declaration

int main() { // begin program

int value; // declaration of integer variablefloat weight; // declaration of float variable

cout << "The constant value of pi is set to: " << pi << “\n”;cout << "Enter your weight in kg: "; // comment asking user for weight valuecin >> weight;cout << "Enter an integer value: "; // comment asking user for weight valuecin >> value;cout << "You have entered " << weight << “ kg as your weight and ”;cout << value << “integer value” ;

return 0;} // end program