C++ is a programming language for building computer programs. It is used for games, apps, and systems. This tutorial takes you from zero to a simple calculator. Every step here works on Arch Linux.
1. Install the compiler
A compiler is a program that turns code into a runnable program. On Arch Linux, install the gcc package. It already includes g++, the compiler for C++.
sudo pacman -S gcc
Check that g++ is installed:
g++ --version
If a version number appears, the compiler is ready.
Do not want to install yet? Use an online compiler instead. Open programiz.com/cpp-programming/online-compiler or cpp.sh in your browser. Write your code there, then press the Run button.
2. Your first program: Hello, world!
Create a file named halo.cpp. Its contents look like this:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}
Here is how to read the code above:
#include <iostream>loads the input-output library, the tool for writing and reading text.using namespace std;lets you writecoutwithout thestd::prefix.int main()is the main function, the point where the program starts running.coutprints text to the screen. The<<sign sends the text to the screen.endlmoves to a new line.return 0;means the program finished without errors.- Every statement ends with a semicolon (
;). A missing semicolon is the most common cause of errors.
3. Compile and run
Save the file, then compile it with g++:
g++ halo.cpp -o halo
This command turns halo.cpp into a program named halo. The -o halo option sets the name of the compiled program.
Run the program:
./halo
The result:
Hello, world!
Note: on Linux, the compiled file does not automatically get a .exe extension. That only happens on Windows. On Linux, run the compiled program with ./program-name.
If compiling fails, read the error message. The message names the line with the problem. Check that line: a missing semicolon, mismatched curly braces, or a mistyped command name.
4. Variables and basic data types
A variable is a named container for storing data. The data type decides what the container holds. The three most common basic types:
int: whole numbers, for example10or-3.double: decimal numbers, for example2.5or3.14.string: text, for example“Briyan”.
How to create variables:
int age = 20;
double height = 1.75;
string name = "Briyan";
A variable’s value can be changed after it is created:
age = 21;
Pick the type that matches the data. Decimal numbers need double, not int. If a decimal number is stored in an int, the decimal part is lost.
5. Basic input and output
cout shows data on the screen. cin reads data from the keyboard. The >> sign sends the user’s typing into a variable.
#include <iostream>
using namespace std;
int main() {
string name;
cout << "What is your name? ";
cin >> name;
cout << "Hello, " << name << "!" << endl;
return 0;
}
This program asks for a name, then greets that name. Text and variables are joined with chained << signs.
Note: cin stops reading when it meets a space. The name “Budi Santoso” is only read as “Budi”. To read a full line, use getline(cin, name).
6. Full example: a simple calculator
This is a complete program. It asks for two numbers, then shows the results of addition, subtraction, multiplication, and division. Copy this code, save it as kalkulator.cpp, then compile and run it.
#include <iostream>
using namespace std;
int main() {
double a, b;
cout << "Masukkan angka pertama: ";
cin >> a;
cout << "Masukkan angka kedua: ";
cin >> b;
cout << "Tambah: " << a + b << endl;
cout << "Kurang: " << a - b << endl;
cout << "Kali: " << a * b << endl;
cout << "Bagi: " << a / b << endl;
return 0;
}
Compile and run:
g++ kalkulator.cpp -o kalkulator
./kalkulator
Example with the numbers 10 and 4:
Masukkan angka pertama: Masukkan angka kedua: Tambah: 14
Kurang: 6
Kali: 40
Bagi: 2.5
The program uses double so division results can be decimal. With int, 10 / 4 gives 2, not 2.5. Dividing by zero causes an error or an infinite value, so never divide by zero.
Congratulations, you can now write your own C++ programs. If you get stuck on a hard error, ask Muse (yue), the AI assistant on this blog.
Comments