|
|
![]() |
![]() |
![]() |
Lesson 2: Basic Input and Output
|
If you cannot see the video below, please install the Adobe Flash Player.
|
|
|
|
|
Basic Input and Output |
|
|
This lesson covers a bit of input and output that will be essential for getting simple programs to work in C++. However, the material should cover most of what we will need for input and output for quite a few lessons. As is customary, we will start with a "Hello World!" program. The original "Hello World" program came from C, and was programmed in the 1970's by Brian Kernighan. Since then, the use of "Hello World!" as a first example program has become the de facto standard in every language.
The "#include" statement pastes a file into this one that gives us access to the input and output objects that we will use. The main function is standard for all C++ console programs. The main function contains the code that will be executed. In this program, "std::cout" starts our only real line of code. The statement "cout" is pronounced c-out and stands for console output. This statement sends our output to the console window, which looks like this:
This window displays the output from the execution of our program. If we zoom in on the upper-left corner, we see that it displays "Hello World!"
One final remark before we move on, "endl" stand for endline, which returns to the beginning of the next line and is equivalent to pressing the "Enter" key in a text editor. The last example demonstrated output. In this one, we show the use of input as well.
In this program, we introduce a variable "iAge" which holds an integer. This integer is where we will store the input. The statement "cin" is pronounced c-in and stands for console input. This takes input from the console window. Here's the execution:
As the execution begins, the question "How old are you?" is sent to the console window to prompt the user for input. The c-in statement halts execution until the users types a number and then presses return. The last c-out statement output the last line of text which includes the "iAge" variable. |
|
![]() |
![]() |
![]() |
| Home | | | Reference | | | Play Games! | | | Forum | | | Site Map | | | Contact Us |






