C++ Syntax, Output & Input

iostream, cout, cin and namespaces.

✍️ 2 exercises in this lesson
  • #include <iostream> gives you input/output streams.
  • std::cout << value prints; chain several values with <<.
  • std::cin >> variable reads input; std::getline(std::cin, line) reads a whole line.
  • "\n" or std::endl ends a line.
  • using namespace std; lets you write cout instead of std::cout (fine for small programs; avoid in headers).

Key Takeaways: C++ Syntax, Output & Input

Mastering C++ Syntax, Output & Input helps you write clean and reliable C++ code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C++ Syntax, Output & Input Code Example

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    int age;

    cout << "Name: ";
    getline(cin, name);
    cout << "Age: ";
    cin >> age;

    cout << "Hello " << name << ", you are " << age << "!\n";
    return 0;
}
Output
Name: Sara Ahmed
Age: 22
Hello Sara Ahmed, you are 22!
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C++ Syntax, Output & Input Practice Exercises

Check what you learned in “C++ Syntax, Output & Input”.

/ 2 solved
Fill in the blank

Read a whole line of input into name.

std::(std::cin, name);
✓ Correct! Not quite. Try again.
Multiple choice

Which operator is used with cin?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services