There have been 2 basic tutorials that the author has written about the tutor for making programs with Borland C+ Builder in X-Code Magazine No. 1, when this tutorial article was written, finding a Borland C++ Builder programming tutor in Indonesian on the Internet was quite difficult so that for those who want to learn Borland C++ Builder it might be quite difficult to find references in Indonesian on the Internet. This is one of the background factors for this tutorial article to be made.
Borland C++ Builder is a visual programming language that is slightly easier to understand according to the author compared to other C language writing, so for you Visual Basic or Delphi programmers who want to move to Borland C++ Builder, it is not too difficult, especially now that there is a Delphi to C++ Builder tool that you can download at www.thinkershome.com, making it even easier for Delphi programmers to use C++ Builder.
Here the author provides 3 basic tutorials for creating simple programs that you can try, in addition, in Borland C++ Builder the source characters are very sensitive, such as lowercase and uppercase letters are distinguished, so in creating programs you have to be more careful.
Happy reading
Create a counting program
First of all, open your Borland C++ Builder program, then select File > New then select Application in the New Items dialog box and the display will appear as below:
Now just follow the table below:
| Komponen | Properti | Nilai |
|----------|---------------|-------------------------|
| Form1 | Caption | Program itung-itungan |
| Label1 | Caption | Nilai X |
| Label2 | Caption | Nilai Y |
| Label3 | Caption | Hasil Perhitungan |
| Edit1 | Text | (Kosongkan) |
| Edit2 | Text | (Kosongkan) |
| Edit3 | Text ReadOnly | (Kosongkan) true |
| Button1 | Caption | &Tambah |
| Button2 | Caption | &Kurang |
| Button3 | Caption | &Bagi |
| Button4 | Caption | &Kali |
| Button5 | Caption | &Reset |
| Button6 | Caption | &Exit |
| TImage | Picture | (Terserah picturenya ?) |
When it's finished, the result will be something like this:
If you have created the form, now you just need to create the source, how do you do it? Follow the instructions below:
Double click on the Add button then enter source:
int x = StrToInt (Edit1 -> Text); int y = StrToInt (Edit2 -> Text);
int hasil = x + y;
Label3 -> Caption = "Hasil X + Y :";
Edit3 -> Text = IntToStr(hasil);
Double click on the Less Button then enter source:
int x = StrToInt (Edit1 -> Text); int y = StrToInt (Edit2 -> Text);
int hasil = x - y;
Label3 -> Caption = "Hasil X - Y :";
Edit3 -> Text = IntToStr(hasil);
Double click on the Share Button then enter the source:
int x = StrToInt (Edit1 -> Text); int y = StrToInt (Edit2 -> Text);
int hasil = x / y;
Label3 -> Caption = "Hasil X / Y :";
Edit3 -> Text = IntToStr(hasil);
Double click on the Kali Button then enter source:
int x = StrToInt (Edit1 -> Text); int y = StrToInt (Edit2 -> Text);
int hasil = x * y;
Label3 -> Caption = "Hasil X x Y :";
Edit3 -> Text = IntToStr(hasil);
Double click on the Reset button then enter source:
Edit1 -> Text = " ";
Edit2 -> Text = " ";
Edit3 -> Text = " ";
Double click on the Exit Button then enter source:
Close();
Now the program is finished, just save it and run it, then if you want to compile it, just compile it.
Ok, now we move on to the next basic tutorial, namely an example of implementing RadioGroup in Borland C++ Builder.
Creating a sample program with RadioGroups
After we create a new form, follow the table below:
| Komponen | Properti | Nilai |
|-------------|------------------------------------------|------------------------------------------------------------------------------------------------------------|
| Form1 | Name Caption | Form1 Media Yogyafree |
| RadioGroup1 | Name Caption Items \| String List Editor | RadioGroup1 Media Yogyafree .. Web Yogyafree .. Milis Yogyafree .. Forum Yogyafree .. Channel Yogyafree .. |
| Button1 | Name Caption | Button1 Tampilkan |
| Button2 | Name | Button2 |
| | Caption | Keluar |
| Label1 | Name Caption | Label1 Media Yogyafree |
| Edit1 | Name Text | Edit1 Dikosongkan |
Once completed, the display will be approximately as below. As an additional item, add the URL address if you want to display the URL address as well:
Once you have finished creating the form, just double click the Display button, then write the source:
switch (RadioGroup1->ItemIndex)
{ case 0 :
{
Edit1->Text =
RadioGroup1->Items->Strings[RadioGroup1->ItemIndex];
} case 1 :
{
Edit1->Text = RadioGroup1->Items->Strings[RadioGroup1->ItemIndex];
} case 2 :
{
Edit1->Text = RadioGroup1->Items->Strings[RadioGroup1->ItemIndex];
} case 3 :
{
Edit1->Text = RadioGroup1->Items->Strings[RadioGroup1->ItemIndex];
} case 4 :
{
Edit1->Text = RadioGroup1->Items->Strings[RadioGroup1->ItemIndex];
}
}
After that double click on the Exit button then enter the source:
Close()
With the 2 basic tutorials above, it is expected that you can know a little about the basic programming of Borlan C++ Builder, now we will create a more functional program, namely an Editor program like Notepad that you might be able to use to create exploits, articles and so on, yes, it feels better to create sources or articles with our own programs from C programming. ?
Create a Text Editor Program by utilizing the RichEdit, MainMenu, SaveDialog and OpenDialog components
In fact, Visual-based Text Editor programs have been around since Microsoft released Microsoft Windows 1.0.x in 1985. Yes, compared to the default Text Editor for Windows, today's Windows are much more advanced in terms of features and others.
Here we will try to create a Text Editor with Borland C++ by utilizing the RichEdit, MainMenu, SaveDialog and OpenDialog components.
First of all follow the specifications from the table below:
| Komponen | Properti | Nilai |
|-------------|------------|----------------------------------------------------------------------|
| Form1 | Name | Form1 |
| | Caption | Editor Text |
| MainMenu1 | Name | MainMenu1 |
| RichEdit1 | Name Lines | RichEdit1 (Bisa dikosongkan atau bisa diisi tulisan terserah anda) |
| OpenDialog1 | Name | OpenDialog1 |
| SaveDialog1 | Ma,e | SaveDialog1 |
If you are still confused about what the Main Menu, Open Dialog and Save Dialog are, the author provides the images below:
After it is created, right-click on the MainMenu then select Menu Designer, after the Form1->MainMenu1 dialog box appears, then fill it in as below:
Well, just make it on the File Menu, as above, here the important thing to fill in is the Caption and the name, remember the name cannot be the same as each other. But if you are still confused about what to fill in, just follow the name and caption as below:
Name : Baru
Caption : &Baru
Name : Buka
Caption : &Buka
Name : Simpan
Caption : &Simpan
Name : Exit
Caption &Exit
To edit, the menu is as below
For better results, follow the name and caption below if this is your first time making it.
Name : Copy
Caption : &Copy
Name : Paste
Caption : &Paste
Name : Cut
Caption : &Cut
Once the Main Menu is complete, we return to the Form.
Once the form is ready, we just need to write the source, how to do it? Click File -> New then fill in the source:
RichEdit1->Lines->Clear();
RichEdit1->Modified=False;
Click File -> Open then fill in the source:
if (OpenDialog1->Execute())
RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
Click File -> Save then fill in the source:
if (SaveDialog1->Execute())
RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
Click File -> Exit then fill in the source:
Close()
Click File -> Copy then fill in the source:
RichEdit1->CopyToClipboard();
Click File -> Paste then fill in the source:
RichEdit1->PasteFromClipboard();
Click File -> Cut then fill in the source:
RichEdit1->CutToClipboard();
After finishing writing the source, save the program and run it to test it, the results are as below:
If you want to compile it, you can compile it into an executable, with the 3 tutorials that the author has written, it is hoped that it can open up a little of your knowledge horizon about Borland C++ Builder programming.
Borland C++ Programming Basics
Substance:
1. COMPILATION PROCESS
2. C++ PROGRAM STRUCTURE
- #include,
- void, main(),
- Statement block,
- cout,
- clrscr();
- Comment
3. BASIC ELEMENTS IN C++
- Identifier,
- Data Types In C++,
- Variables and Constants,
- Operators and expressions,
- Input in C++
- Manipulator
C++ Programming Basics
1. Compilation Process
C++ programs are written with the extension .cpp. In order to be executed or run, the program must first be compiled using the C++ compiler. The compilation process is; the source file (.cpp) together with the header file (.h) are first translated by the C++ compiler into object code (.obj), the format of this object file is binary (coded 0 and 1). Furthermore, all object files together with library files (.lib) are linked together by the linker and the result is an Executable file.
Fig. the process of creating an executable file
2. C++ Program Structure
#include <nama_file>
void main()
{
<blok_pernyataan>
}
#include
It is a preprocessor directive that functions to instruct the compiler to insert files, when the program is compiled. Usually the files inserted are header files.
void in front of main()
Used to indicate that the main() function has no return value.
main()
It is the main function that is the beginning and end of C++ program execution.
Sign ()
Used to enclose function arguments, namely the values to be passed to the function.
Statement block
A statement block can consist of one or more statements and ends with a ( sign, for example when declaring 3 variables with the same data type, such as; int a, b, c;
Example program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Program yang mengandung blok pernyataan
*/
void main()
{
clrscr(); //membersihkan layar
cout << "Selamat Belajar C++\n";
getch();
}
Execution result:
Statement Block
To compile a program using Borland C++, you can press Ctrl + F9 or this button:
cout (pronounced "c out")
It is an object in C++ that is used to direct data to standard output (screen).
<< sign
The two less-than signs are "insertion/putting" operators that will direct the operand (data) located to the right of it to the object located to the left of it.
In the example above, "Happy Learning C++\n" is directed to cout, which displays the string on the screen. \n is the new line character.
#include <iostream.h>
Instructs the compiler to include the iostream.h file when the program is compiled without ending with a semicolon. The iostream.h file needs to be included in programs that involve cout. Without # include < iostream.h > there will be an error when the program is compiled. Because the iostream.h file contains declarations needed by cout and various objects related to input-output operations.
clrscr();
Statement required to clear the screen. If using this statement then the header file conio.h must be included.
Comment
It is necessary to explain / provide annotations related to the program or parts of the program. The contents of the explanation are usually:
- Purpose/function of the program
- When the program is created/revised
- Other information about the use of a number of statements in the program
There are 2 ways to comment on C++ programming, namely;
- It starts with // (two slashes). All text after // is considered a comment and will not be executed by C++.
- It starts with the /* comment block sign and ends with the */ sign. To provide comments on more than 1 line, however, this form is often used to ignore / disable a number of program lines or statements that have been made for a reason.
Example program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Program yang mengandung komentar
*/
void main()
{
clrscr(); //membarsihkan layar <--- ini adalah komentar
// teks ini sampai akhir baris tidak akan pernah dieksekusi <--- ini adalah komentar
cout << "Selamat Belajar C++\n";
getch();
}
3. Basic Elements of C++
3.1 Identifier
Identifiers are names that will be used in programming, which are usually used to indicate variables, constants, data types, functions, labels, objects and other things created by programmers.
An identifier is a combination of letters, numbers and underscores (_). The identifier name must begin with a letter or underscore and use a word that is easy to understand and can represent the function of the identifier created. Identifiers in C++ are case sensitive or differentiated between lowercase and uppercase letters. For example, the identifiers gajibasok, GajiBakok, GAJIPOKOK, are three different identifiers.
3.2 Data Types In C++
Regarding data types, you can read more here:
UNDERSTANDING DATA TYPES, VARIABLES AND CONSTANTS
Example program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Program yang mengandung tipe data dalam c++
*/
void main()
{
clrscr();
char huruf;
huruf = 'B';
cout<<"Isi huruf bertipe char = "<<huruf<<'\n';
int angka;
angka = 123;
cout<<"Isi angka bertipe int = "<<angka<<'\n';
long jumlah;
jumlah = 12345678;
cout<<"Isi jumlah bertipe long = "<<jumlah<<'\n';
float nilai;
nilai = 234.543;
cout<<"Isi nilai bertipe float = "<<nilai<<'\n';
double cacah;
cacah = 3453.345;
cout<<"Isi cacah bertipe double = "<<cacah<<'\n';
long double total;
total = 23456.3945;
cout<<"Isi total bertipe long double = "<<total<<'\n';
getch();
}
Execution Results:
C++ Data Types
3.3 Variables and Constants
In the programming process, data types are commonly used to define a variable or constant. A variable is a memory allocated with a specific name and can only accommodate data according to the specified type. The nature of a variable is that the value it contains will be easily changed according to the process that occurs, such as the example below. While a constant is a memory allocated with a specific name that contains a value that has a fixed nature that cannot be changed.
Before a variable is used, it must first be defined. Variables can be defined anywhere before the variable is used in the form:
tipevariabel namavariabel;
To enter a value into a variable, use the form:
variabel = nilai;
Example program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Program yang mengandung pemasukkan nilai pada variabel
*/
void main()
{
int bilangan; //Mendefinisikan variabel bilangan bertipe int
bilangan = 10; //Memberikan nilai 10 pada variabel bilangan
clrscr();
cout<<"Isi bilangan = "<<bilangan<<'\n';
bilangan = 50;
cout<<"Isi bilangan = "<<bilangan<<'\n';
getch();
}
Execution result:
Variables
To declare a constant, use the form:
const datatypeconstantname = constantvalue;
Example program:
#include <iostream.h>
#include <conio.h>
/**
*gatewan.com
*Wawan Beneran
*Program yang mengandung konstanta
*/
void main()
{
const float phi = 3.14;
int jari = 11;
float luas;
clrscr();
luas = phi * jari * jari;
cout<<"Luas lingkaran = "<<luas<<'\n';
getch();
}
Execution result:
Constants
In C++, there is a known variable modification using unsigned and signed. Variables that are added unsigned will cause the value contained in the variable to be positive, while signed does not cause a change in the value of the data contained (the same as the basic data value).
| Modifikasi Tipe Data | Persamaan | Jangkauan Nilai |
|----------------------|-----------------------------|----------------------------------|
| unsigned char | tidak ada | 0 s/d 225 |
| unsigned int | unsigned | 0 s/d 65.535 |
| unsigned short int | unsigned short | 0 s/d 65.535 |
| unsigned long int | unsigned long | 0 s/d 4.294.967.295 |
| signed char | char | -128 s/d 127 |
| signed int | int | -32.768 s/d32.767 |
| signed short int | short, signed short | -32.768 s/d32.767 |
| signed long int | long, long int, signed long | -2.147.483.648 s/d 2.147.483.687 |
To understand more deeply, see the observations below;
How to Find the Range of Integer and Unsign Integer Data Types in C++
3.4 Operators and expressions
Operators are symbols that will be used to perform an operation or manipulation. To produce a value, an operator must receive an operand. While an expression is a combination of an operator and an operand that produces an expression value, for example:
6 + 10 -- 4
The expression above consists of two operators, namely + (addition) and - (subtraction) and three operands, namely 6, 10 and 4, the value of the expression above is 12.
Arithmetic Operators
| Operator | Keterangan | Contoh |
|----------|--------------------------|--------|
| * | Perkalian | 10 * 5 |
| / | Pembagian | 20 / 4 |
| % | Sisa pembagian (modulus) | 22 % 3 |
| + | Penjumlahan | 5 + 6 |
| - | Pengurangan | 8 - 4 |
Example program:
#include<iostream.h>
#include<conio.h
/**
*bundet.com
*Penggunaan operator aritmatika
*/
void main()
{
int d,b,a,c;
b = 7;
a = 5;
c = 2;
d = b * b - 4 * a * c;
cout<<"Nilai d = "<<d<<'\n';
//mencari sisa bagi
int sisa;
sisa = 20 % 3;
cout<<"Sisa 20 % 3 = "<<sisa<<'\n';
getch();
}
Execution result:
Arithmetic Operators
Assignment Operator
The assignment operator uses the equals symbol (=) which functions to assign a value to a variable. Examples of using the assignment operator in C++:
| Contoh | Keterangan |
|-------------------------|------------------------------------------------------------------------------------------------------------------|
| y = 4 + ( a = 7 ); | Variabel a diberi nilai 7, lalu y diisi nilai ungkapan 4 + 7 |
| a = b = c = d = e = 10; | Pertama e diisi dengan 10, lalu d diisi dengan nilai e, lalu c diisi dengan nilai d, dan a diisi dengan nilai b. |
Example program:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Penggunaan operator penugasan
*/
void main()
{
int a,b,c,d,e;
a = b = c = d = e = 100;
cout<<"Isi a = "<<a<<'\n';
cout<<"Isi b = "<<b<<'\n';
cout<<"Isi c = "<<c<<'\n';
cout<<"Isi d = "<<d<<'\n';
cout<<"Isi e = "<<e<<'\n';
int x,y;
x = 10 * (y = 3);
cout<<"Isi x = "<<x<<'\n';
getch();
}
Execution result:
Assignment Operator
Increment and decrement operators
Used to increase and decrease the value of variables of type integer. The form of this operator is ++ (increase) and -- (decrease). For example:
x = x + 1;
y = y - 1;
Can be written:
y--;
x++;
or
--y;
++x;
Example program:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Penggunaan operator penaikkan dan penurunan
*/
void main()
{
int a,b,c;
a = 15;
//variabel a dinaikkan 1 baru dijumlahkan dengan 10
b = 10 + ++a;
cout<<"Isi b = "<<b<<'\n';
//variabel b dijumlahkan dengan 10 baru b dinaikkan 1
c = 10 + b++;
cout<<"Isi b = "<<b<<'\n';
cout<<"Isi c = "<<c<<'\n';
getch();
}
Execution result:
C++ Increment and Decrement Operators
Bitwise operators
Used to manipulate binary numbers.
| Operator | Keterangan | Contoh |
|----------|--------------------|---------|
| << | Geser bit ke kiri | 10 << 3 |
| >> | Geser bit ke kanan | 10 >> 3 |
| & | Bitwise and | 10 & 3 |
| \| | Bitwise or | 10 \| 3 |
| ^ | Bitwise xor | 10 ^ 3 |
| ~ | Bitwise not | ~ 10 |
Bitwise operator precedence
| Prioritas | Operator |
|-----------|----------|
| Tinggi | ~ |
| Normal | << >> |
| | & |
| | ^ |
| Rendah | \| |
Example program:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Penggunaan operator bitwise
*/
void main()
{
unsigned long a,b,x;
a = 50;
b = 3;
x = a << b;
cout<<"Hasil "<<a<<" << "<<b<<" = "<<x<<'\n';
x = a >> b;
cout<<"Hasil "<<a<<" >> "<<b<<" = "<<x<<'\n';
x = a & b;
cout<<"Hasil "<<a<<" & "<<b<<" = "<<x<<'\n';
x = a | b;
cout<<"Hasil "<<a<<" | "<<b<<" = "<<x<<'\n';
x = a ^ b;
cout<<"Hasil "<<a<<" ^ "<<b<<" = "<<x<<'\n';
x = ~a;
cout<<"Hasil ~"<<a<<" = "<<x<<'\n';
getch();
}
Execution result:
C++ Bitwise Operators
Compound operators
Compound operators are used to shorten the writing of an assignment.
x = x + 5;
y = y -- 8;
Can be written:
x += 5;
y -= 8;
Example program:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Penggunaan operator majemuk
*/
void main()
{
int a;
a = 20;
cout<<"Nilai a sekarang = "<<a<<'\n';
a += 5;
cout<<"Nilai a += 5 sekarang = "<<a<<'\n';
a -= 3;
cout<<"Nilai a -= 3 sekarang = "<<a<<'\n';
a *= 2;
cout<<"Nilai a *= 2 sekarang = "<<a<<'\n';
a %= 3;
cout<<"Nilai a %= 3 sekarang = "<<a<<'\n';
a <<= 1; //a = a << 1;
cout<<"Nilai a <<= 1 sekarang = "<<a<<'\n';
a >>= 1; //a = a >> 1;
cout<<"Nilai a >>= 1 sekarang = "<<a<<'\n';
getch();
}
Execution result:
C++ Compound Operators
Relational Operators
Relational operators are used to compare two values, the result of which is a value of 1 if true and a value of 0 if false.
| Operator | Keterangan |
|----------|-------------------------------|
| == | Sama dengan (bukan penugasan) |
| != | Tidak sama dengan |
| < | Lebih kecil |
| > | Lebih besar |
| <= | Lebih kecil atau sama dengan |
| >= | Lebih besar atau sama dengan |
Example program:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Penggunaan operator relasi
*/
void main()
{
int a,b,hasil;
a = 45;
b = 25;
hasil = a == b;
cout<<"Hasil relasi "<<a<<" == "<<b<<" = "<<hasil<<'\n';
hasil = a != b;
cout<<"Hasil relasi "<<a<<" != "<<b<<" = "<<hasil<<'\n';
hasil = a > b;
cout<<"Hasil relasi "<<a<<" > "<<b<<" = "<<hasil<<'\n';
hasil = a < b;
cout<<"Hasil relasi "<<a<<" < "<<b<<" = "<<hasil<<'\n';
hasil = a >= b;
cout<<"Hasil relasi "<<a<<" >= "<<b<<" = "<<hasil<<'\n';
hasil = a <= b;
cout<<"Hasil relasi "<<a<<" <= "<<b<<" = "<<hasil<<'\n';
getch();
}
Execution result:
C++ Relational Operators
Logical Operators
Logical operators are used to connect two conditions into one condition.
Example program:
#include<iostream.h>
#include<conio.h>
/**
*gatewan.com
*Wawan Beneran
*Penggunaan operator logika
*/
void main()
{
int hasil;
cout<<"Operator logika && (dan)"<<'\n';
hasil = (5 > 2) && (4 > 3);
cout<<"Hasil logika (5 > 2) && (4 > 3)= "<<hasil<<'\n';
hasil = (5 < 2) && (4 > 3);
cout<<"Hasil logika (5 < 2) && (4 > 3)= "<<hasil<<'\n';
hasil = (5 > 2) && (4 < 3);
cout<<"Hasil logika (5 > 2) && (4 < 3)= "<<hasil<<'\n';
hasil = (5 < 2) && (4 < 3);
cout<<"Hasil logika (5 > 3) && (4 > 3)= "<<hasil<<'\n';
cout<<"\nOperator logika || (atau)"<<'\n';
hasil = (5 > 2) || (4 > 3);
cout<<"Hasil logika (5 > 2) || (4 > 3)= "<<hasil<<'\n';
hasil = (5 < 2) || (4 > 3);
cout<<"Hasil logika (5 < 2) || (4 > 3)= "<<hasil<<'\n';
hasil = (5 > 2) || (4 < 3);
cout<<"Hasil logika (5 > 2) || (4 < 3)= "<<hasil<<'\n';
hasil = (5 < 2) || (4 < 3);
cout<<"Hasil logika (5 > 3) || (4 > 3)= "<<hasil<<'\n';
cout<<"\nOperator logika !"<<'\n';
hasil = !(5 > 2);
cout<<"Hasil logika !(5 > 2) = "<<hasil<<'\n';
hasil = !(5 < 2);
cout<<"Hasil logika !(5 < 2) = "<<hasil<<'\n';
getch();
}
Execution result:
C++ Logical Operators
Condition Operator
Conditional operators are used to get a value from two possibilities based on a condition. The form of the conditional operator:
Expression1?expression2:expression3
If expression1 is tested to be true then the result is expression2, if false then the result is expression3.
Example program:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Penggunaan operator kondisi
*/
void main()
{
int angka1, angka2, maks;
angka1 = 70;
angka2 = 90;
maks = (angka1 > angka2)?angka1:angka2;
cout<<"Angka terbesar = "<<maks<<'\n';
getch();
}
Execution result:
C++ Conditional Operators
3.5 Input in C++
1. cout (read C out)
It is an object in C++ that functions to display data to the standard output (screen). The cout object has been widely used in the examples in front that are used to display results to the screen.
General form:
cout << var;
2. cin (read C in)
It is an object in C++ that functions to read data from the keyboard.
General form:
cin >> var;
Function to read data from the keyboard and enter it into a variable named var.
Example program:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Penggunaan cin
*/
void main()
{
int angka;
char huruf;
cout<<"Masukkan angka : ";
cin>>angka;
cout<<"Masukkan sebuah huruf : ";
cin>>huruf;
cout<<"\nAngka yang anda masukkan : "<<angka<<'\n';
cout<<"Huruf yang anda masukkan : "<<huruf<<'\n';
getch();
}
Execution result:
C++ Cin
cin >> cannot be used to read Space or Tab characters, if you want to read these keys, use the getch() function.
3. getch() and getche() functions
The getch() and getche() functions are used to read without having to press enter, besides this function can also read the Space and Enter keys.
General form:
character = getch();
character = getche();
If the getch() and getche() functions are included, the conio.h header must be included. The getch() function does not display the character of the pressed key, while getche() displays the character of the pressed key.
Example program:
#include<iostream.h>
#include<conio.h>
/**
*bundet.com
*Penggunaan getch() dan geche()
*/
void main()
{
char kar1, kar2;
cout<<"Tekan sembarang karakater : ";
kar1 = getch();
cout<<"\nTekan sembarang karakater : ";
kar2 = getche();
cout<<"\n\nKarakter pertama : "<<kar1<<'\n';
cout<<"Karakter kedua : "<<kar2<<'\n';
getch();
}
Execution result:
C++ getch and getche
3.6 Manipulator
Manipulators are used to control the display of data on the screen. Some manipulators in C++:
1. Endl manipulator
The endl manipulator is used to insert a newline character. This manipulator is identical to the '\n' character.
Example program:
#include<iostream.h>
#include<conio.h>
/**
*gatewan.com
*Wawan Beneran
*Penggunaan endl
*/
void main()
{
int angka1 = 100;
int angka2 = 5000;
float total = 12323.8;
cout<<"Isi angka 1 : "<<angka1<<endl;
cout<<"Isi angka 2 : "<<angka2<<endl;
cout<<"Isi angka 2 : "<<total<<endl;
getch();
}
Execution result:
C++ endl
2. setw() manipulator
The setw() manipulator is used to set the width of the data display on the screen. If you are going to set the data display on the screen, you need to include the iomanip.h header.
Example program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
/**
*bundet.com
*Penggunaan setw()
*/
void main()
{
int angka = 1005;
cout<<setw(0)<<angka<<endl;
cout<<setw(4)<<angka<<endl;
cout<<setw(7)<<angka<<endl;
getch();
}
Execution result:
C++ setw
3. The setfill() manipulator
The setfill() manipulator is used to set the characters filled in the fields specified by setw() that are not used to display data.
Example program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
/**
*bundet.com
*Penggunaan setw() dan setfill()
*/
void main()
{
int angka = 1005;
cout<<setw(0)<<setfill('*')<<angka<<endl;
cout<<setw(4)<<setfill('*')<<angka<<endl;
cout<<setw(6)<<setfill('*')<<angka<<endl;
cout<<setw(8)<<setfill('*')<<angka<<endl;
getch();
}
Execution result:
C++ setfill
4. Dec, oct and hex manipulator
The dec, oct and hex manipulators are used to display data in decimal (base 10), octal (base 8) and hexadecimal (base 16) formats.
Example program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
/**
*bundet.com
*Penggunaan dec, oct dan hex
*/
void main()
{
int x = 150;
cout<<"Nilai x : "<<x<<endl;
cout<<"Nilai x dalam oktal : "<<oct<<x<<endl;
cout<<"Nilai x dalam heksadesimal : "<<hex<<x<<endl;
cout<<"Nilai x dalam desimal : "<<dec<<x<<endl;
getch();
}
Execution result:
C++ dec, oct and hex
5. Manipulator setiosflags()
The setiosflags() manipulator is a manipulator that can be used to control a number of format flags as follows:
| Tanda Format | Keterangan |
|-----------------|-------------------------------------------------------------------------------------|
| ios::left | Menyetel rata kiri terhadap lebar field yang diatur melalui setw() |
| ios::right | Menyetel rata kanan terhadap lebar field yang diatur melalui setw() |
| ios::scientifie | Memformat keluaran dalam notasi eksponensial |
| ios::fixed | Memformat keluaran dalam bentuk notasi desimal |
| ios::dec | Memformat keluaran dalam basis 10 (desimal) |
| ios::oct | Memformat keluaran dalam basis 8 (oktal) |
| ios::hex | Memformat keluaran dalam basis 16 (heksadesimal) |
| ios::uppercase | Memformat huruf pada notasi heksadesimal dalam bentuk huruf kapital |
| ios::showbase | Menampilkan awalan 0x untuk bilangan heksadesimal atau 0 (nol) untuk bilangan oktal |
| ios::showpoint | Menampilkan titik desimal pada bilangan pecahan yang tidak memiliki bagian pecahan |
| ios::showpos | Untuk menampilkan tanda + pada bilangan positif |
Example program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
/**
*bundet.com
*Penggunaan setiosflags()
*/
void main()
{
int x = 520;
cout<<"x ditampilkan dengan ios::left "<<endl;
cout<<setiosflags(ios::left)<<setw(10)<<x<<endl<<endl;
cout<<"x ditampilkan dengan ios::right "<<endl;
cout<<setiosflags(ios::right)<<setw(10)<<x<<endl<<endl;
float y = 123.456;
cout<<"y ditampilkan dengan ios::fixed "<<endl;
cout<<setiosflags(ios::fixed)<<y<<endl<<endl;
cout<<resetiosflags(ios::fixed);
cout<<"y ditampilkan dengan ios::scientific "<<endl;
cout<<setiosflags(ios::scientific)<<y<<endl<<endl;
cout<<resetiosflags(ios::scientific);
int bil = 51;
cout<<"Tanpa ios::showbase : "<<endl;
cout<<oct<<bil<<endl;
cout<<dec<<bil<<endl;
cout<<hex<<bil<<endl;
cout<<"Dengan ios::showbase : "<<endl;
cout<<setiosflags(ios::showbase);
cout<<oct<<bil<<endl;
cout<<hex<<bil<<endl;
cout<<dec<<bil<<endl;
cout<<"\nDengan ios::uppercase untuk heksadesimal : "<<endl;
cout<<setiosflags(ios::uppercase)<<bil<<endl;
cout<<resetiosflags(ios::showbase);
cout<<resetiosflags(ios::uppercase);
float a = 234.00;
cout<<"\nTanpa ios::showpoint : "<<endl;
cout<<a<<endl<<endl;
cout<<"Dengan ios::showpoint : "<<endl;
cout<<setiosflags(ios::showpoint)<<a<<endl;
cout<<resetiosflags(ios::showpoint);
int b = 27;
cout<<"\nTanpa ios::showpos : "<<endl;
cout<<b<<endl<<endl;
cout<<"Dengan ios::showpos : "<<endl;
cout<<setiosflags(ios::showpos)<<b<<endl;
cout<<resetiosflags(ios::showpos);
getch();
}
Execution result:
C++ setiosflags()
6. Resetiosflags() manipulator
The resetiosflags() manipulator is used to return to the initial format after using the manipulator, for example:
setiosflags(ios::left)
has been used, then to return to the initial form you can use:
resetiosflags(ios::left)
Example program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
/**
*bundet.com
*Penggunaan resetiosflags()
*/
void main()
{
int x = 12340;
cout<<"Ditampilkan dengan ios::right "<<endl;
cout<<setiosflags(ios::left)<<setw(10)<<x<<endl<<endl;
cout<<"Setelah dilakukan resetiosflags(ios::right)"<<endl;
cout<<resetiosflags(ios::left);
cout<<setw(10)<<x<<endl<<endl;
getch();
}
Execution result:
C++ resetiosflags()
7. Manipulator setprecision()
The setprecision() manipulator is used to set the number of fractional digits to be displayed in fractional numbers.
General form:
setprecision(n)
Where n is the number of digits to be displayed.
Example program:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
/**
*bundet.com
*Penggunaan setprecision()
*/
void main()
{
float y = 12.340;
cout<<setiosflags(ios::fixed);
cout<<setprecision(0)<<y<<endl;
cout<<setprecision(1)<<y<<endl;
cout<<setprecision(2)<<y<<endl;
cout<<setprecision(3)<<y<<endl;
cout<<setprecision(4)<<y<<endl;
cout<<setprecision(5)<<y<<endl;
getch();
}
Execution result:
C++ setprecision()
Source:
Algorithm & Programming Module, Compiled by Mr. Eko Riswanto, ST, M.Cs,
Comment 1
- ZONY YULFADL Jul I6 2015, 16:49:00 = interesting and educational blog, can you share it with my students, sir?
- ZONY YULFADL I6 Jul 2015, 16:50:00 = May I share this with other friends, Mr. Gatewan?
- ALHIKMATU KHOIRUDIN 10 Jul 2015, 06:48:00 = I am self-taught, my major in college is not this but a hobby he he and this blog has a good discussion, thank you for sharing your knowledge.
- MOHAMMAD NUR SULAEMAN Sep 27, 2015, 23:23:00 = Excuse me :-) May I print it? just to be my little notes and to study at home.. thank you in advance.
- UNKNOWN Oct 1 2015, 13:33:00 = can I accommodate it sir nice blog
- SEPRI PANJAITAN 4 Nov 2015, 14:37:00 = I'm still learning C++, the discussion here is good, may I bookmark it first, sir?
- SETIAWAN PUTRA 7 Dec 2015, 19:25:00 = thanks ....
- KRISNA CAHYANTO 26 Dec 2015, 07:00:00 = If I may ask, I would like to ask about the differences in the file menu ==> New ==> 1. Text project 2. project 3. app expert 4. resource project. What are the differences between these 4? Thank you if you are willing to answer.
- UNKNOWN 1 Jan 2016, 11:53:00 = Sir... I'll copy the article for studying at home.
- Want to learn programming language.
- RAFLI HAKIM Jan 13, 2016, 21:17:00 = why don't the examples use cin? only 1 example uses cin, and that is not the result of 2 numbers
- IDA BAGUS KRISNA Feb 13, 2016, 15:19:00 = This is great! Use it to study before college while still in high school. Thanks!
- DEDE IQBAL 3 Mar 2016, 20:45:00 = Bro, I want to ask what type of comparison data is there? Addition, subtraction, etc., please tell me.
- TONY PUTRA 13 Mar 2016, 09:51:00 = very useful bro, we need to add more, if possible to the binar tree
- MUHAMMAD ULYA Mar 15, 2016, 20:29:00 = Bro, why isn't the example program showing up? It was there yesterday, but just now when I opened it again, the example program didn't show up.
- ADLY MUNTHEA May 16, 2016, 05:09:00 = Crazy, really cool.. Actually, it's a shame, sir, this knowledge is expensive. They are free without donations, just hoping that the ads will be clicked.. But I am happy with this good person.. May you be blessed, sir.
- FEBRIAN RAMADHANI 16 Jun 2016, 19:41:00 = if for example you want to find the nominal value from 1 - 100, what is the program, sir? Please provide the program, sir.
- UNKNOWN 18 Sep 2016, 22:16:00 = Excuse me, I want to ask sir, if I want to create a function "if x is between 2 and 5" which if written mathematically is (2<=x<=5) and should be in c++ as if(2<=x<=5){} but the function doesn't work if I enter the number 1 but continues the if function above, please help me sir
- DEVANY SANDRA Oct 19 2016, 20:17:00 = permission ctrl+C
- AJI NOOR SASONGKO Oct 20, 2016, 09:01:00 = thanks bro, very helpful, can I copy it for learning? thank you in advance
- UNKNOWN 29 Oct 2016, 18:35:00 = thank you for the knowledge that is very useful for me, especially it is very helpful.
- CANDRA MULADO Oct 30, 2016, 09:42:00 = wow, this is very helpful, thank you, bro. I want to ask, bro, what is the purpose of putting and returning? thanks. =)
- CANDRA MULADO 30 Nov 2016, 22:33:00 = thanks bro for the enlightenment
- AHMAD SYUKRAN Oct 31, 2016, 20:21:00 = create a program question about a mother shopping at the supermarket to buy household necessities, the available money is 500 thousand, the mother wants to buy: 1 kg of rice 15 thousand, 1 kg of granulated sugar 12 thousand, 1 kg of detergent soap 15 thousand, 1 liter of cooking oil 23 thousand, 1 toothpaste 20 thousand, determine how many items of the shopping she has above with the mother's money, what operator does the question above use if I may know?
- SANCE AINUL YAQIN 13 Dec 2016, 04:12:00 = additional yes.. also added multiplication operands for items with quantities of more than one, for example 1 kg of rice = 15k => a=15k;
- if b is input then c=a*b; cout<<"Total"<<c;
- SANCE AINUL YAQIN Dec 13 2016, 04:07:00 = cool, proud..very good..
- EKORISWANTOFAMILY 29 Dec 2016, 20:17:00 = Amazing.. Thank you, respected Mr. Eko Riswanto, good luck, Mr. Lecturer.
- RULIYANI16 29 Dec 2016, 21:10:00 = thank you, it's very useful for learning..
- ERIXON SARUKSUK Feb 24 2017, 12:23:00 = permission to copy, sir?
- MN ROHMAN 7 Mar 2017, 20:18:00 = permission to copy, bro
- OBAY SOBARNAS May 4 2017, 16:23:00 = permission to copy paste too
- TEGUH SAPUTRA Jul 5, 2017, 10:20:00 = useful, thank you
- IRWAN PROGRAMMER 11 Aug 2017, 23:44:00 = very useful knowledge, thank you for sharing your knowledge
- REZA YOYOY 28 Sep 2017, 09:01:00 = permission to post, sir, for learning materials and blog assignments
- DWI CAHYO 4 Oct 2017, 07:46:00 = Awesome bro, can share with friends who don't know Borland hehe
- ARIO NOVRA Dec 12, 2017, 16:57:00 = thank you
- SLAMET TRIYADI Feb 14 2018, 03:13:00 = sorry bro, can I copy and paste it to my blog for my college assignment
- FAJAR PRAYOGA May 17, 2018, 00:54:00 = Very helpful sir, is there any material on linked lists and C++ coding and what is the function of the -> sign, thank you
- VHIYA FI May 17, 2018, 13:05:00 = Sir, are you a Borland C++ programmer? Where do you live? If possible, can you help me create a program for a report?
- Hello Mr. Zony Y, Yes sir, please do so with pleasure.
- Hello Alhikmatu Khoirudin, you're welcome, it's a pleasure to be able to provide benefits.
- Hello Moh. Nur S. Yes, please do so with pleasure.. good luck, thanks
- Hii Unknown, yes, please do so with pleasure, good luck, thanks
- Hello Sepri P. Wow, glad to be of help, thank you, please do so with pleasure.
- you are welcome
- Okay, please go ahead.. and happy studying..
- Because the above is just the initial material, bro, if you want to know an example of a program that uses cin, please study it at a more advanced material level, here -> C++ REPETITION PROGRAM COLLECTION
- Hello IDA BAGUS KRISNA, That's right, please use it for studying, so that when you enter the first semester, you can easily follow... Okay, happy studying and good luck.
- To find out how to use operators for comparison, please see the following example -> Example of a C++ Program to Find Maximum and Minimum Values (simple)
- Hi Tony P. Okay bro, thank you very much for your input, next time if something I learn is related to this material, then I will add it bro, I will update it again, like I did in my old posts.. thank you and happy learning..
- Oh yeah, that's because it's still hosted on github, while my github is still in the reconstruction process... okay, I'll fix it soon, thanks.
- Hello Adly Munthea, thank you for your appreciation, my motivation to build this blog is not because of the benefits, but more for educational purposes, you can read more on the About page .
- Hi Febrian, you can learn the concept of the program from the following example -> C++ PROGRAM EXAMPLE FOR RANDOM PRIME NUMBERS 0 - 100
- Hii UNKNOWN, that's not how it works, in programming you can't enter functions or mathematical equations directly, you need to declare variables, etc. An example of the function above should be like this: if (int a=3; a<=5; a++){...}
- Hello Devany Sandra, please go ahead.
- Hello Aji Noor, please go ahead.
- Hii Abdullah Yusuf, you're welcome, thanks for that, happy learning
- puts is used to print a string to standard output (stdout) while adding a new line automatically ('\n' or <<endl , so it's actually similar to cout<<""; and printf(), except that puts adds a new line. return to return the value to its source, for example: I have a variable a, and that variable goes into the Kuadrat_Dimas_Kanjeng function, so axa = a (returned to the a container, but with the contents already doubled)
- You're welcome CANDRA MULADO
- It's very simple, bro, if you only ask about the number of items, just using the addition operator is enough. Happy learning & good luck!
- Hii SANCE AINUL YAQIN, Thank you very much for your participation, great.
- Hi SANCE AINUL YAQIN, you are even cooler
- Thank you, respected Mr. Eko Riswanto, good luck, Mr. Lecturer.
- Okay, you're welcome, Miss RULIYANI16, study hard, okay?
- Hii ERIXON SARUKSUK, okay, please, please.
- Hi MN ROHMAN, please come along
- Hello OBAY SOBARNAS, okay bro, please go ahead
- You're welcome, Uncle TEGUH SAPUTRA
- you're welcome, uncle IRWAN PROGRAMMER
- Hi REZA YOYOY, okay
- please go ahead, Mr. DWI CAHYO
- masama
- Ok, please do so, but please include the source.
- Yes, you can check it in the sitemap menu or in the search box.
- I live in www miss