
Basically, a computer program consists of only three parts: input-process-Output
An input is something that we input into a program.
Input is usually taken from the input devices such as keyboard, mouse, camera, microphone, etc.
Processes are the steps a program must take to produce output.
Output is the information produced after the process is performed. Output is usually displayed to the computer screen.
In the C++ programming language, there are several basic functions to display output and retrieve input.
What the hell is that?
Let's discuss…
Output Function on C++
C++ has four basic functions to display output:
cout to display text to screen;
2.cerr to display error;
3.clog to display logs;
4.printf() to display output, this function is from C;
We will focus on only two, namely cout and printf().
You see cerr and clog, how to wear the same as cout.
The difference is in the context of its use, namely for errors and logs.
Cout function
Cout function is the standard function on C++ to display output to screen.
Here is the basic structure of the cout function:

After the << symbol we can write the text to be displayed to the screen.
The text must be enclosed with quotation marks and to create a new line can use the endl or symbol \n.
The creation of a new line is optional, whether we want to add it or not.
Example:
cout << "My name is ";
cout << "mr.website 7";
The output will be displayed in one line:
My name is mr.website 7
If we use endlor \n.
cout << "My name is "<< endl;
cout << "mr.website 7";
The results will be displayed in two lines:
My name is
mr.website 7
Anything after the << symbol will be displayed to the screen. If we want to display a variable, we can write it like this:
string name \= "mr.website 7";
cout << "Hello " << name << endl;
Result:
Hello mr.website 7
Easy ‘kan?
Let's try practicing.
Create a program called program_output.cpp, then fill it with the following code:
#includes
using namespace std;
int main(){
cout << "\=\=\=\=\=\=\=\=\=\=\= PROGRAM OUTPUT \=\=\=\=\=\=\=\=\=\=" =< endl;
cout << "This progarm is a program for\n";
cout << "Display output to computer screen.\n";
cout << "Output is very important to display\n";
cout << "information to user.\n";
cout << "\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\\\=\=\=\=\=\=\=" << endl;
cout << endl;
cout << "This program was created by Dian" << endl;
return 0;
}
Try compile and run, though,
Printf() function
Printf() function is a function that is originally from C language, but can also be used on C++.
Printf() function is a function to display output to a computer screen.
This function is in the library and also .
Here is the basic structure of the printf() function:
printf("format",.).;
Watch:
"format" is a text (string) to display. Then the ... sign will contain a variable or value to be displayed based on the format given in the "format" text".
Let's look at an example:
Create a C++ program called program_output_printf.cpp, then fill it with the following code.
#includes
using namespace std;
int main(){
printf("Hello, this is the output text\n");
printf("My name is %s\n", "Dian");
printf("My age %d\n", 20);
return 0;
}
There are some things to look out for…
In the printf() function we use the symbols %s, %d, and \n for the text format.
Let's discuss the meaning of the symbol:
%s is a symbol for displaying string values;
%d is a symbol to display the value of a number or decimal number;
\n is a symbol for creating a new line.
In addition to the three symbols, there are many more symbols.
SymbolArt or Function
%c. to display characters
%s to display text (string)
%d, %i to display decimal number
%f to display fractional numbers
%o to display octal number
%x to display hexadecimal number
\t to create tabs
Input Function on C++
While to take input, C++ has a cin function and can also use scanf() from C language.
Cin function
The cin (c input) function is a function to take input from the keyboard.
Here is the basic form of the cin function:
cin>> variable;
The cin function requires a variable to store the inputted data.
We will discuss the variables later in:
Learn C++ #05: Getting to Know Variables, Constants, and Data Types
In essence, variables function to store data when the program is running.
Let's try using the cin function.
Create a new program called program_input.cpp, then fill it with the following code:
#includes
using namespace std;
int main(){
name string;
cout << "Write name: ";
cin >> name;
cout << "Hi " << name;
cout << "elamat come to the club!" << endl;
return 0;
}
After that, try compiling and running.
Function scanf()
The scanf() function is actually from C language, but it can also be used on C++.
The scanf() function is a function to take input from the keyboard. This function has a format such as printf() function.
scanf("%s", &var);
The format given depends on what type of data type we want to take.
For example, if you want to take a number, you can use %d or %i.
Let's look at an example:
#includes
using namepsace std;
int main () {
// create variables
char name[20], web_address[30];
printf("Name: ");
scanf("%s", &name);
printf("Web address: ");
scanf("%s", &web_address);
printf("\n-----------------------------\n");
printf("Inputted name: %s\n", name);
printf("Instituted Web Address: %s\n", web\_address);
return 0;
}
To solve this problem, we can change the format used on scanf() to like this:
printf("Name: ");
scanf("%[^\n]s", name);
Then the scanf() function will receive a space.
In using scanf(), we are encouraged to use the symbol & before the variable name.
Example:
#includes
void main(){
int a, b, c;
printf("Insert value a: ");
scanf("%i", &a);
printf("Insert value b: ");
scanf("%i", &b);
c \= a \+ b;
printf("Result a \+ b: %i", c);
}
 Ahmad Muhardian 20 Dec 2019
Learn C++ #04: Know the Input and Output Functions on C++
#C++

Basically, a computer program consists of only three parts:

An input is something that we input into a program.
Input is usually taken from the input devices such as keyboard, mouse, camera, microphone, etc.
Processes are the steps a program must take to produce output.
Output is the information produced after the process is performed. Output is usually displayed to the computer screen.
In the C++ programming language, there are several basic functions to display output and retrieve input.
What the hell is that?
Let's discuss…
Output Function on C++
C++ has four basic functions to display output:
cout to display text to screen;
cerr to display error;
clog to display logs;
printf() to display output, this function is from C;
We will focus on only two, namely cout and printf().
You see cerr and clog, how to wear the same as cout.
The difference is in the context of its use, namely for errors and logs.
Cout function
Cout function is the standard function on C++ to display output to screen.
Here is the basic structure of the cout function:

After the << symbol we can write the text to be displayed to the screen.
The text must be enclosed with quotation marks and to create a new line can use the endl or symbol \n.
The creation of a new line is optional, whether we want to add it or not.
Example:
cout << "My name is "; cout << "Farmer code";
The output will be displayed in one line:
My name is Code Farmer
If we use endlor \n.
cout << "My name is " << endl; cout << "Farm code";
The results will be displayed in two lines:
My name is Code Farmer
Anything after the << symbol will be displayed to the screen. If we want to display a variable, we can write it like this:
string name \= "Farm Code"; cout << "Hello " << name << endl;
Result:
Hello Farmers Code
Easy ‘kan?
Let's try practicing.
Create a program called program_output.cpp, then fill it with the following code:
#include using namespace std; int main(){ cout << "\=\=\=\=\=\=\=\=\=\=\=\= PROGRAM OUTPUT \=\=\=\=\=\=\=\=\\\\\=\=" << endl; cout << "Progarm this is a program for\n"; cout << "Display output to computer screen.\n"; cout << "Output is very important to display\n"; cout << "information to user.\n"; cout << "\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\\\\\=\=\=\=\=\=\=\=\=\=\=\=\=\=" =< endl; cout << endl; cout << "This program was created by Dian" << endl; return 0; }
Try compile and run, then the result:

Printf() function
Printf() function is a function that is originally from C language, but can also be used on C++.
Printf() function is a function to display output to a computer screen.
This function is in the library and also .
Here is the basic structure of the printf() function:

Watch:
"format" is a text (string) to display. Then the ... sign will contain a variable or value to be displayed based on the format given in the "format" text".
Let's look at an example:
Create a C++ program called program_output_printf.cpp, then fill it with the following code.
#include using namespace std; int main(){ printf("Hello, this is text output\n"); printf("My name %s\n", "Dian"); printf("My age %d\n", 20); return 0; }
Result:

There are some things to look out for…
In the printf() function we use the symbols %s, %d, and \n for the text format.
Let's discuss the meaning of the symbol:
%s is a symbol for displaying string values;
%d is a symbol to display the value of a number or decimal number;
\n is a symbol for creating a new line.
In addition to the three symbols, there are many more symbols.
SymbolArt or Function%cto display character%sto display text (string)%d, %ito display decimal number%fto display fractional number%oto display octal number%xto display hexadecimal number\tto create tabs
Input Function on C++
While to take input, C++ has a cin function and can also use scanf() from C language.
Cin function
The cin (c input) function is a function to take input from the keyboard.
Here is the basic form of the cin function:

The cin function requires a variable to store the inputted data.
We will discuss the variables later in:
Learn C++ #05: Getting to Know Variables, Constants, and Data Types
In essence, variables function to store data when the program is running.
Let's try using the cin function.
Create a new program called program_input.cpp, then fill it with the following code:
#include using namespace std; int main(){ string name; cout << "Write name: "; cin >> name; cout << "Hi " << name; cout << " elamat came to the club!" << endl; return 0; }
After that, try compiling and running.
The result: then:

Function scanf()
The scanf() function is actually from C language, but it can also be used on C++.
The scanf() function is a function to take input from the keyboard. This function has a format such as printf() function.

The format given depends on what type of data type we want to take.
For example, if you want to take a number, you can use %d or %i.
Let's look at an example:
#include using namepsace std; int main () { // create variable char name[20], web_address[30]; printf("Name: "); scanf("%s", &name); printf("Web address: "); scanf("%s", &web_address); printf("\n------------------------\n"); printf("Instituted name: %s\n", name); printf("Inpublished Web Address: %s\n", web_address); return 0; }
Result:


To solve this problem, we can change the format used on scanf() to like this:
printf("Name: "); scanf("%[^\n]s", name);
Then the scanf() function will receive a space.
In using scanf(), we are encouraged to use the symbol & before the variable name.
Example:
#include void main(){ int a, b, c; printf("Input value a: "); scanf("%i", &a); printf("Include value b: "); scanf("%i", &b); c \= a + b; printf("Result a + b: %i", c); }
Symbol & serves to retrieve the memory address of a variable.
The scanf() function requires a place to store the value to be entered.
Therefore we give the symbol & in front of the variable name to determine the memory address to be used by scanf().
What Next?
Well that's the basic functions used to take input and display output on C++.
We can use the built-in function C++ or any function of C language such as printf(), scanf(), puts() etc.
I prefer to use cin to take input and printf() to display output.
What about you?
Oh yes, next please learn about:
Learn C++ #05: Getting to Know Variables, Constants, and Data Types