Digital Reference---Getting Started
You are about to take your first step into C++
language. We, at Digital Reference, will guide you all the
way to making a complete, working program. Let
us get a feel of C++ by making a simple program together.
The following rules are very essential to building a program:
Rule No. 1:
Always start your program first by making
comments. Comments give you a general idea of what your
program does. For example, if you open your .cpp file (C
Plus Plus file), you will see a bunch of codes. You may not
remember what kind of program that file was written for, and then,
you will have to look through the entire code to decipher what
kind of program it is. Therefore, commenting programs will
be an advantage.
In order to make comments, type:
/*type comments
about your program here between the asterisks and the slashes*/
To make comments anywhere in your program in
order to recognize what your code does, you can type before or
after your code in such a manner:
//your comment here
Rule No. 2:
After writing the comment for the program, you
should type on the next line:
#include <iostream.h>
This statement means to include the iostream
library. In C++, a library is an assortment of codes that
can be utilized by the program to do certain assignments. In
the case of the iostream, this library allows the program to input
and output. (iostream means input output stream.)
The section where you type #include <header
file> is called the preprocessor.
Hey there, how’s it going so far? Let’s get
into the main part of the program, shall we?
Rule No. 3:
On the next line, type:
int main()
{
}
What you see above in red is the main function.
A function consists of lines of codes that tells the program what
to do. The main function IS the core of the entire program. The
codes that you will type between the braces will be the
assignments of the program. int means that the function will
return an integer. After writing the statements/assigning tasks to
the program, you want to put on the line before the closing brace:
return(0);
return(0); means that you want the main function
to return the integer 0. When the program returns the integer 0,
it means that the program can run successfully.
Okay, here comes some FUN! We will
now assign the program to do something! Excited? We sure hope so!
Rule No. 4:
In order to have the program display something
on the screen, you can type within the main function:
cout << "Type message
to be displayed here";
cout means console output stream. You want your
message to be displayed. In another words, let us say you want
your message to flow through the stream and out onto the screen.
The << points the direction that the message should flow
towards. << are called insertion operators. The semicolon is
ALWAYS used after a statement. Otherwise, you will have an error
in your program.
Now, here’s the goodie! We will construct your
FIRST PROGRAM. Let’s have the program display the message,
"Hi, this is your first working program!"
/*This is your first program
displaying a message.*/
#include <iostream.h>
int main()
{
cout
<< "Hi, this is your first working program!";
return(0);
}
Rule No. 5:
When you are typing on your word processor, you
press return to go to the next line. However, in C++, if you want
to display something on the next line, you need to type endl.
endl means "end of line." Using endl,
you can display anything on the next line.
For example:
If we want the program to display,
Hi!
How are you?
One way to write the code to display that on the
screen is:
#include <iostream.h>
int main()
{
cout
<< "Hi!" << endl;
cout
<< "How are you?" << endl;
return(0);
}
Rule No. 6:
To have the user of the program enter some data,
you need to include cin. cin stands for console
input stream. You also need to include the extraction operators
>>. As you can see, these operators point in, unlike the
insertion operators used with cout.
Let’s make another simple program asking the
user to enter data.
#include <iostream.h>
int main()
{
int Age;
cout
<< "How old are you?";
cin
>> Age;
cout
<< endl << "You are " << Age <<
" years old." << endl;
return(0);
}
CONGRATULATIONS! You have just made a successful
program. You ought to be very proud of yourself to have come to
this stage. KEEP UP THE GOOD WORK! Please stay with us, and you
will learn many more interesting codes that can help you elaborate
your existing program.
|