How to input/output in C++ (CPP, C Plus Plus)
When you type at the keyboard and your program takes your input as data that is standard input. When your output is displayed at the terminal screen that is standard output. The standard input stream is called cin, The standard output stream is called cout. To get basic I/O functionality, you must #include < iostream > library in header of C++ program.Standard Output
By default. the term standard output refers to the output display on the screen. C++ language uses the cout stream object to display standard display. The word ‘cout‘ stands for console output. The cout object is used with the insertion operator (<<).Examples of cout:
Printing “Same to Same” Message
1 2 3 4 5 6 |
#include <iostream> using namespace std; main() { cout<<"C++ Programming"; } |
Printing Values without Variable
1 2 3 4 5 6 |
#include <iostream> using namespace std; main() { cout << 245; } |
Printing Values with variables:
1 2 3 4 5 6 7 |
#include <iostream> using namespace std; main() { int price=200; cout << price; } |
Printing message with variables:
1 2 3 4 5 6 7 |
#include <iostream> using namespace std; main() { int price=200; cout<<"The Value of Price"<<price; } |
Standard Input
By default, the term standard input refers to the input given vie keyboard. C++ language uses cin stream object to get standard input. The word ‘cin‘ stands for console input. C++ handles standard input by applying the extraction operator (>>) with cinstream. The operator must be followed by a variable or array etc. The variable/array etc are used to store the input data.Examples: Example 1:
1 2 3 4 5 6 7 |
#include <iostream> using namespace std; main() { int y; cin>>y; } |
1 2 3 4 5 6 7 8 9 |
#include <iostream> using namespace std; main() { int x; int y; int z; cin>>x>>y>>z; } |