Back home Next view contents in a pop up window
A simple cout cin
program
Few
basic things you need to know before we get started.
Take a look of this program
for a moment. Its ok if you do not understand any part of it. Just take a look
at the source code and the output. This program Asks from the user to
enter two numbers and then it displays the numbers that were entered along with
a text message.
If you reached the end of the page and havent understood some of the things in this page is it ok. I would advise you though to keep coming back at this page since these explenations will help you later on in a GREAT WAY.
#include<iostream.h>
// Line 1
int main()
//Line 2
{
// line 3
int num1, num2;
// Line 4
cout << " hello User. Please enter two numbers:" << endl;
cin >> num1 >> num2;
cout << " User you have entered the numbers "
<< num1 << " and " << num2 << endl;
return 0;
}
//Line 9
output
| hello User. Please enter two numbers: 1 2 User you have entered the number 1 and 2 |
This is
a simple program. But lets see some of the components.
Line 1: iostream is a Header file because its
placed outside at the top. The iostream contains data definitions
and operations that are used for inputting and outputting data. Anything that
begins with the # symbol is called preprocessor.
Preprocessors-->
The header files become part of our program by the preprocessor include
directive. The preprocessor is the first tool that processes our source and
it passes it to the compiler. Then the compiler takes over. The preprocessor
directive starts with the pound sing (#). Then the include
command is placed. If
the file name is placed with in brackets (<>) that means that the file
with in the brackets is a standard file. If the file is within quotation marks
(" " ) then the file is a user created header file.
Line 2: The int
main(). The two parentheses
intricate that main
is a function with an empty argument list. C++ programs begin execution of main
even if its not the first function they encounter. The int
determines what type
of value will be returned.
Line 3 Until Line 9: This is what we call block of code. What are
in the brackets are the instructions that tell the computer what to do.
Line 4: The int
tell that the computer that num1 and num2 are variables of an integer type. In
other words if we type something else besides an integer number an error will
occur because the computer expects two ints.
We can set variables of the same type with a comma separating them. A semicolon
is placed at the end to indicate that there are no more variables in that line.
|
int---->
Integer value.
Its a zero or any positive or negative number with out a decimal point. Float - double - long double. The difference between these numbers lays on the amount of memory of memory each needs to be stored. double float Uses twice as much space than float uses. long double Uses twice as much space than double float uses. char-->Character Values ( Character data). Expects a character. Includes the letter of the alphabet, the ten digits through 0 to 9 and characters such as the minus sing the percentage sing and so forth. |
Line 5: cout outputs on the monitor whatever is told to output. The syntax is always this: cout <<
Line 6:
cin Expects
an input by the user. The syntax is always this: cin >>
Its common sense that the cin command should be placed after a cout command that prompts the user with directions on what to input.
Note: Always place a semicolon after you have finished a command line. The program will know that the line has stoped and than a new of code is fallowing. Take notice the semicolons in this program. View it as giving directions to a blind person. " Go 5 feet straight, now stop ( ; ) . New direction, turn left 45 degrees and walk 4 feet. Now stop ( ; ). New command....and so forth.
Back home Next view contents in a pop up window