Labs ICT
โญ Pro Login

Input & Output

2 min read | C++ Tutorial
โญ

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

Input and output are how your program talks to the world. In C++, you use cout to send text to the console and cin to read input from the user. You also have endl to insert a new line and flush the output buffer.

cout and cin

cout uses the << operator โ€” think of it as an arrow pointing toward cout, meaning data flows to the console. cin uses the >> operator โ€” data flows from the keyboard into your variable.

The endl (end line) moves the cursor to the next line. You can also use \n inside a string to do the same thing, but endl also flushes the output, which can be helpful.

#include <iostream>
using namespace std;

int main() {
  string name;
  int age;

  cout << "Enter your name: ";
  cin >> name;

  cout << "Enter your age: ";
  cin >> age;

  cout << "Hello, " << name << "!" << endl;
  cout << "You are " << age << " years old." << endl;
  cout << "Nice to meet you!";
  return 0;
}

There is a catch with cin โ€” it stops reading at whitespace. So if you type "Bilal Mubarak" and press enter, only "Bilal" will be stored in the string. To read a whole line including spaces, use getline(cin, variable) instead. We will cover that in a later lesson.

๐Ÿงช Quick Quiz

Which operator is used with cin to read user input?