Termux Work & Programming Language

Termux Work & Programming Language
Learn C++ #05: Getting to Know Variables and Data Types



The core of a computer program is to accept input, perform processing, and produce output.


Input values can be obtained from the keyboard, files, camera, microphone, and so on.


While the output can be displayed to the monitor, print to a document, or into a file.


At the processing stage, the program needs the help of variables to store temporary values.


Just like the time we think, we need some memory to process information.


In this tutorial, I will discuss thoroughly how to use variables and data types in C++ programming.


Ready it?


Let's get started…


 Ahmad Muhardian 29 Sep 2017


Learn C++ #05: Getting to Know Variables and Data Types


#C++


The core of a computer program is to accept input, perform processing, and produce output.



Input values can be obtained from the keyboard, files, camera, microphone, and so on.


While the output can be displayed to the monitor, print to a document, or into a file.


At the processing stage, the program needs the help of variables to store temporary values.


Just like the time we think, we need some memory to process information.


In this tutorial, I will discuss thoroughly how to use variables and data types in C++ programming.


Ready it?


Let's get started…


What are Variables and Data Types?


All running computer programs will store data temporarily in RAM (Random Access Memory).


The data stored in RAM has an address represented in the hexa desmial number.


How do programs store value to RAM?


The answer is by using variables.


The more variables you create, the greater the memory that will be used in RAM.


So we can conclude…


A variable is the name of a storage location in memory.


Data type is the type of data that is stored in variables.


The various data types in C++ can be seen from the following table.


How to Create Variables in C++


Variable creation or variable declaration in C++ we can do like this:


name string;


int age;


The data type is written first, then followed by the name of the variable.


FYI: for C++11 we can use auto data type as placeholder for unclear data type.


The variables above will store the value of null (empty), because we have not filled.


We can also create variables by filling them in directly.


Example:


string name \= "mr.website 7";


high float \= 175.43;


For more details, let's try to practice in the program.


Creating a C++ Porgram with Variables and Data Types


Please create a new file called biodata.cpp, then fill it with the following code:


#includes


using namespace std;


int main(){


// variable data type declaration


name string;


int age;


// -- input process ---


cout << "Who is your name?" << endl;


cout << "answer: ";


// saving data to variable


getline(cin, name);


cout << "How old are you?" << endl;


cout << "answer: ";


cout << "answer: ";


// saving data to variable


cin >> age;


cout << "Gender \[L/P\]: ";


// saving data to variable


cin \>\> type\_sex;



// -- process output ---


cout << "Greetings, " << name << " Now you are aged ";


cout << age << " and you are gender "<< type\_sex;


return 0;


}


After that do the compilation and execution of the program.


Easy right?


Describe the donk program above!


Well I'll explain.


First, it starts with creating a variable.


name string;


int age;


In this line of code, we create three variables with different data types.


Then we fill the value based on the input given from the keyboard with the cin command.


cin >> age;


cin >> type_sex;


Especially for string data types, we use the getline() function to take a single row of inputted values.


getline(cin, name);


Finally we display the contents of the variable with the cout command.


Changing Data Type Name


We can change the name of the data type ourselves with the typedef command.


For example:


typedef int numbers;


That is, int data type (integer) we will make the name into numbers.


Then, to use it we can write like this:


age number;


What Next?


We already know the variables and data types, what next?


Next please learn about:


Learn C++ #06: Using an Operator