C++ Programming Language (Console)
by EN. Michael Bidollahkhany
Part 1
Purposes :
1- How we can Install Borland C++ Builder 6
2- Some Tricks about How we can Save Our Works at C++ Builder (& Notepad ++ 6.3)
3- Some Important Notes about a Programming Language
4- Some Important Tips about Programming Base
1- How We can Install Borland C++ Builder 6
Picture 1-a
2- Some Tricks about How we can Save Our Works at C++ Builder (& Notepad ++ 6.3)
2-1- The Popular format of C++ Units (Our Programming Source file) is " .cpp ", because If you want to Use Notepad to Write and Save your Source files, You most save them at this format like Picture 2-1-a
Picture 2-1-a
2-2- Install Notepad++, This program Can help you to Manage better your Source, There are some good features of Notepad++ 6.3:
Easy for Find and Replace, Counting Lines and words , ... of your Source file, Some compile tools for Finding source Problems, Coloring your Keywords and Sorting your Source Parts and etc..
2-3- If you want use Borland C++ Builder for Write and Saving your source code, You most:
2-3-1- a New blank page for coding:
2-3-1-1- Open File menu
2-3-1-2- New
2-3-1-3- Other...
2-3-1-4- and Select Console Wizard (Picture 2-3-1-a)
2-3-1-5- and Fallow the Picture 2-3-1-b
Picture 2-3-1-a
Picture 2-3-1-b
2-3-2- Saving your Program's Source:
2-3-2-1- Open File menu
2-3-2-2- Select Save All
2-3-2-3- Select Path (Choose where you want save your Program)
2-3-2-4- Click on Save Button
3- Some Important Notes about a Programming Language
3-1- Arrangement at coding
Each programmer should has a arrangement, because a good arrangement can ease finding your coding problems, Now you can some examples that say to us why our coding need arrangement... .(Picture 3-1-a)
Picture 3-1-a
3-2- Each program needs a Special path( a special folder for save them)
Your programs may don't Run or Compile, If you save them at same place.
4- Some Important Tips about Programming Base (Your first Programs)
We now introduce C++ programming, which facilitates a disciplined approach to program
design. Most of the C++ programs you’ll study in this blog, process information and display
results. In this part, We present Five examples that demonstrate how your programs
can display messages and obtain information from the user for processing. The first Tree
examples simply display messages on the screen. The next obtains two numbers from a
user, calculates their sum and displays the result. The accompanying discussion shows you
how to perform arithmetic calculations and save their results for later use. We analyze each program one line at a time to help you ease your way into C++ programming.
design. Most of the C++ programs you’ll study in this blog, process information and display
results. In this part, We present Five examples that demonstrate how your programs
can display messages and obtain information from the user for processing. The first Tree
examples simply display messages on the screen. The next obtains two numbers from a
user, calculates their sum and displays the result. The accompanying discussion shows you
how to perform arithmetic calculations and save their results for later use. We analyze each program one line at a time to help you ease your way into C++ programming.
First Step:
We now consider a simple program that prints a line of text. This program illustrates several important features of the C++ language.
1 //Program 1 (Printing a Word)
2 #include <iostream.h>
3 #include <conio.h>
4
5 int main()2 #include <iostream.h>
3 #include <conio.h>
4
6 {
7 cout << "Welcome";
8
9 getch();
10 return 0;
11 }
Descriptions:
line1. " // " sign is for disabling a line, In another word With this sign compiler understand this line isn't a Source line and Compiler won't Compile it.
line2. " # " sign is for the name of header files that We used them in Our source code(, e.g: "cout" is in "iostream" header file or "getch()" is in "conio" header file), Header file's format is ".h" because We type their name with ".h" in C++ builder.
line5. "intmain()" is Title of Our Program that Our Program's source code will type at It.
line6. " { " or " } ": In each Order if We have more than One line coding, we'll write them in two " { " and " } " signs.
line7. "cout << " use for printing a line or sentence to screen.
line7. ' " ' sign use when We want to print or use a Character (each word or sign is a character) .
line8. "getch()", With this word We make computer to take s.t from User, for e.g "getch()" will take an Enter to go on the Program.
line10. "return 0", Each Procedure that is "int" or etc types (We'll describe them at next Parts), needs to "return" s.t.
What is the difference between Programing in Microsoft Visual Studio and Borland C++ Builder?
We have a skeleton (I named that!) for C++ programing... .At C++ Builder:
#include <iostream.h>
#include <conio.h>
int main()
{
// Our Code
getch();
return 0;
} At Visual Studio:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int main()
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
// Our Code
getch();
return 0;
}
And This Difference is in Their Skeleton!
Step 2:
1 //Program 2 (Printing a Text)
2 #include <iostream.h>
3 #include <conio.h>
4
5 int main()2 #include <iostream.h>
3 #include <conio.h>
4
6 {
7 cout << "\tWelcome";
8 cout << "\tTo C++\n";
9
10 getch();
11 return 0;
12 }
Descriptions:
line7. "\t" :
The backslash (\) is called an escape character. It indicates that a “special” character is to be output. When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence. The escape sequence \n means newline. It causes the cursor (i.e., the current screen-position indicator) to move to the beginning of the next line on the screen. Some common escape sequences are listed in Table 4-s2-a.
Escape
sequence Description
\n Newline. Position the screen cursor to the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\' Single quote. Use to print a single quote character.
\" Double quote. Used to print a double quote character.
sequence Description
\n Newline. Position the screen cursor to the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\' Single quote. Use to print a single quote character.
\" Double quote. Used to print a double quote character.
line8. "\t..\n": You can use each escape character in ' " ' signs.
Step 3:
1 //Program 3 (Printing multiple lines of text with a single statement.)
2 #include <iostream.h>
3 #include <conio.h>
4 int main()
5 {
2 #include <iostream.h>
3 #include <conio.h>
4 int main()
5 {
6 cout << "Welcome\nto\nC++!\n";
7
8 getch();
9 return 0;
10 }
10 }
Step 4:
1 //Program4 (Addition program that displays the sum of two integers.)
2
2
3 #include <conio.h>
4 #include <iostream.h>
5 int main()
4 #include <iostream.h>
5 int main()
6 {
7 // variable declarations
8 int number1;
9 int number2;
10 int sum;
11
7 // variable declarations
8 int number1;
9 int number2;
10 int sum;
11
12 cout << "Enter first integer: "; // prompt user for data
13 cin >> number1;
14
15 cout << "Enter second integer: "; // prompt user for data
16 cin >> number2;
17
18 sum =number1+number2;
19
20 cout << "Sum is " << sum << endl; // display sum
21
22 getch();
23 return 0;
24 }
13 cin >> number1;
14
15 cout << "Enter second integer: "; // prompt user for data
16 cin >> number2;
17
18 sum =number1+number2;
19
20 cout << "Sum is " << sum << endl; // display sum
21
22 getch();
23 return 0;
24 }
Descriptions:
line8. "int number1":
The identifiers number1, number2 and sum are the names of variables. A variable is a location in the computer’s memory where a value can be stored for use by a program. These declarations specify that the variables number1, number2 and sum are data of type int, meaning that these variables will hold integer values, i.e., whole numbers such as 7, –11, 0 and 31914. All variables must be declared with a name and a data type before they can be used in a program. Several variables of the same type may be declared in one declaration or in multiple declarations. We could have declared all three variables in one declaration by using a comma-separated list as follows:
1 int number1, number2, sum;
Fundamental Types
We’ll soon discuss the type double for specifying real numbers, and the type char for specifying
character data. Real numbers are numbers with decimal points, such as 3.4, 0.0 and
–11.19. A char variable may hold only a single lowercase letter, a single uppercase letter, a
single digit or a single special character (e.g., $ or *). Types such as int, double and char
are called fundamental types. Fundamental-type names are keywords and therefore must appear
in all lowercase letters. Appendix C contains the complete list of fundamental types.
Identifiers
A variable name (such as number1) is any valid identifier that is not a keyword. An identifier
is a series of characters consisting of letters, digits and underscores ( _ ) that does not
begin with a digit. C++ is case sensitive—uppercase and lowercase letters are different, so
a1 and A1 are different identifiers.
We’ll soon discuss the type double for specifying real numbers, and the type char for specifying
character data. Real numbers are numbers with decimal points, such as 3.4, 0.0 and
–11.19. A char variable may hold only a single lowercase letter, a single uppercase letter, a
single digit or a single special character (e.g., $ or *). Types such as int, double and char
are called fundamental types. Fundamental-type names are keywords and therefore must appear
in all lowercase letters. Appendix C contains the complete list of fundamental types.
Identifiers
A variable name (such as number1) is any valid identifier that is not a keyword. An identifier
is a series of characters consisting of letters, digits and underscores ( _ ) that does not
begin with a digit. C++ is case sensitive—uppercase and lowercase letters are different, so
a1 and A1 are different identifiers.
line13. "cin >>":
Uses the standard input stream object cin (of namespace std of visual studio) and the stream extraction
operator, >>, to obtain a value from the keyboard. Using the stream extraction operator with cin takes character input from the standard input stream, which is usually the
keyboard. We like to pronounce the preceding statement as, “cin gives a value to
number1” or simply “cin gives number1.”
operator, >>, to obtain a value from the keyboard. Using the stream extraction operator with cin takes character input from the standard input stream, which is usually the
keyboard. We like to pronounce the preceding statement as, “cin gives a value to
number1” or simply “cin gives number1.”
line18. "sum =number1+number2":
Adds the values of variables number1 and number2 and assigns the result to variable sum using the assignment operator =. The statement is read as, “sum gets the value of number1 + number2.” Most calculations are performed in assignment statements. The = operator and the + operator are called binary operators because each has two operands. In the case of the + operator, the two operands are number1 and number2. In the case of the preceding = operator, the two operands are sum and the value of the expression number1 + number2.
line20. "cout << " " << << endl":
Displays the character string Sum is followed by the numerical value of variable sum followed by endl—a so-called stream manipulator. The name endl is an abbreviation for “end line” and belongs to namespace std at visual studio. The endl stream manipulator outputs a newline, then “flushes the output buffer.” This simply means that, on some systems where outputs accumulate in the machine until there are enough to “make it worthwhile” to display them on the screen, std::endl forces any accumulated outputs to be displayed at that moment. This can be important when the outputs are prompting the user for an action, such as entering data.
Arithmetic:
C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus operator can be used only with integer operands. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2. In later chapters, we discuss many interesting applications of the modulus operator, such as determining whether one number is a multiple of another (a special case of this is determining whether a number is odd or even).
e.g:
Step 5:
Have a Good Time
No comments:
Post a Comment