The gateway to C++ I/O. Provides std::cin, std::cout, std::cerr, and std::clog. Use using namespace std; for study, or prefix with std:: in production code.
Include: #include <iostream> | Namespace: std | Compile: g++ -Wall -std=c++17 ...
| Name | Type / Signature | Description |
|---|---|---|
| std::cout | std::ostream | Standard output stream (to terminal) |
| std::cin | std::istream | Standard input stream (from keyboard) |
| std::cerr | std::ostream | Standard error stream (unbuffered) |
| std::clog | std::ostream | Standard log stream (buffered) |
| std::endl | ostream manip | Insert newline and flush the stream |
| std::flush | ostream manip | Flush the stream without a newline |
| std::ws | istream manip | Skip leading whitespace in input |
| operator<< | ostream & | Stream insertion operator (output) |
| operator>> | istream & | Stream extraction operator (input) |
| std::getline | istream &getline(istream &, string &, char delim='\n') | Read full line into string |
Copy, save as demo.cpp, and compile with the command at the bottom.
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello, C++ World!" << endl;
cerr << "This goes to stderr" << endl;
string name; int age;
cout << "Enter your name: "; cin >> name;
cout << "Enter your age: "; cin >> age;
cout << "Hello, " << name << "! Age: " << age << "\n";
cin.ignore();
string sentence;
cout << "Enter a sentence: ";
getline(cin, sentence);
cout << "You typed: " << sentence << "\n";
return 0;
}
// Compile: g++ -Wall -std=c++17 -o iostream_demo iostream_demo.cpp