Last Updated on 2021-05-06 by Clay
No matter which programming language, we all need to be familiar with Data Input and Output. In C++, we can use the <iostream>
header file to master the basic I/O operation.
iostream, which is the abbreviation of Input/Output Stream.
Standard Output (cout)
cout
is the most common output method, which can print out data of many type, and use it with the <<
symbol.
#include <iostream> #include <string> using namespace std; int main() { int a = 5; float b = 3.3; char c = 'a'; string d = "test"; cout << "int: " << a << endl; cout << "float: " << b << endl; cout << "char: " << c << endl; cout << "string: " << d << endl; return 0; }
Output:
int: 5
float: 3.3
char: a
string: test
Standard Input (cin)
Relative to cout
, cin
is standard input, usually in the form of inputting a string from the keyboard, and it needs to be used with symbols such as >>
.
#include <iostream> using namespace std; int main() { // Settings char name[100]; // Input cout << "What's your name?" << endl; cin >> name; // Output cout << "\nYour name is: " << name; return 0; }
Output:
What's your name?
Clay
Your name is: Clay
The message can be printed! However, if you challenge a little, you will find that if the entered name is blank, for example, John Wick, then the name will only print John.
To solve this problem, a part of the concept of loop may be mentioned, maybe I will leave it to later.
printf()
printf()
also outputs the message to the screen, and also returns the "number of characters" transmitted. If you want to specify variables as output in printf()
, you need to match the so-called format specifier.
The specifier tables that can be referred to are listed below:
Specifier | Output |
---|---|
%d, %i | Signed decimal integer |
%u | Unsigned decimal integer |
%o | Unsigned octal integer |
%x | Unsigned hexadecimal integer |
%X | Unsigned hexadecimal integer (uppercase) |
%f | Decimal floating point number (lower case) |
%F | Decimal floating point number (uppercase) |
%e | Scientific notation (lower case) |
%E | Scientific notation (uppercase) |
%g | Use the shortest form (%e or %f) |
%G | Use the shortest form (%E or %F) |
%a | Hexadecimal floating point number (lower case) |
%A | Hexadecimal floating point number (uppercase) |
%c | character |
%s | String |
%p | Pointer address |
%% | Write a single% sign |
The following is a simple demonstration:
#include <iostream> using namespace std; int main() { printf("%%c: %c\n", 'C'); printf("%%s: %s\n", "Today is a nice day."); printf("%%d: %d\n", 10); printf("%%f: %f\n", 22.31); return 0; }
Output:
%c: C
%s: Today is a nice day.
%d: 10
%f: 22.310000
You can try to print something different.
scanf()
In contrast to printf()
, scanf()
takes user input and uses the &
symbol to address the variable input type.
#include <iostream> using namespace std; int main() { // Setting int num; // Input printf("Enter a number: "); scanf("%d", &num); // Output printf("Your input is: %d", num); return 0; }
Output:
Enter a number: 100
Your input is: 100
References
Read More
- [Linux][C++] How to compile and execute a C++ program
- [C++] Tutorial(1): Data Type
- [C++] Tutorial(2): Arithmetic Expression
- [C++] Tutorial(3): Data Input and Output
- [C++] Tutorial(4): if-else, switch, loop and flow control
- [C++] Tutorial(5): File Read and Write
- [C++] Tutorial(6): Pointer and Reference
- [C++] Tutorial(7): Functions