Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic Pre-processor cout To output a message.

Similar presentations


Presentation on theme: "Topic Pre-processor cout To output a message."— Presentation transcript:

1 Topic Pre-processor cout To output a message

2 A sample C++ Program // sample C++ program #include <iostream>
comment // sample C++ program #include <iostream> using namespace std; int main() { cout << "Hello World!"; return 0; } compiler directive use standard namespace beginning of function named main beginning of block for main output statement Pp55-58 string literal send 0 to operating system; exit main function end of block for main

3 cout Used to print out a message on the screen Not a keyword in C++
In order to use it, you have to include the file that defines cout --- “iostream.h” Use “#include <iostream>” to do it DEFINE BEFORE USE. pp55 – header file pp58, cout

4 The #include Directive
Inserts the contents of another file into the program Do not place a semicolon at end of #include line pp55

5 g++ g++ is a 3-in-1 command A preprocessor will A preprocessor
A compiler A linker A preprocessor will Remove #include directives Replace with copies of header files

6 using namespace std; A namespace is a collection of names.
Every name has to be unique Uses standard namespace called std. Ends with a semicolon Follows #include directives in the code Must appear in all programs pp56

7 Function main int main ( ) { // function body return 0; }
Starting point of the program Exactly one main function per program pp56-57

8 Function A collection of related statements
Performs a specific operation int Specify return type of the function Returns an integer when function finishes ( ) Parameters of the function Empty ( ) means “no parameter needed” pp57

9 cout Displays output on computer screen
Used together with the stream insertion operator << to send output to screen: cout << "Programming is fun!"; << can be used multiple times in an instruction cout << “Programming " << “is fun!"; Or: cout << “Programming "; cout << “is fun!"; pp58

10 Two ways to start a new line
use the \n escape sequence (inside quotation marks) cout << "Programming is\nfun!"; Or use the endl manipulator (outside quotations marks) cout << "Programming is" << endl; cout << "fun!"; pp66

11 The endl Manipulator Do NOT put quotation marks around endl
The last character in endl is a lowercase L, not the number 1. endl This is a lowercase L

12 They will produce Programming is fun!

13 Escape characters Used within double quotes
Don’t get printed literally \n starts a new line \t inserts a tab \\ prints a backslash \ \” prints a double quote \’ prints a single quote pp66

14 What does this program output?
// This program outputs a few sentences. #include <iostream> using namespace std; int main() { cout << “The works of Wolfgang\ninclude the following"; cout << “\nThe Turkish March” << endl; cout << “and Symphony No. 40 “; cout << “in G minor.” <<endl; return 0; } => This code will generate 5 lines of output

15 Structure of your program
Include directives Using namespace Write all instructions inside main( ) function Compiler ignores all blank spaces except those inside quotation marks

16 Review C++ language elements How to use cout
Section 2.1 pages 54-59, 66 How to use cout


Download ppt "Topic Pre-processor cout To output a message."

Similar presentations


Ads by Google