Back to track
#85 Easy Foundations

cin and cout

Open the editor

Problem

Read a name and an age, then print "NAME is AGE years old". The name is a single word with no spaces.

Constraints

The name is at most 50 characters and contains no whitespace. 0 <= age <= 200

Input

Line 1: a word. Line 2: an integer.

Output

One line: NAME is AGE years old

The idea

Concept

cin >> on a string stops at the first whitespace, so it reads one word rather than one line. Reading a whole line needs getline, which is why mixing the two takes care.

Approach

Extract the word and the number, then compose the sentence.

Hints

  1. 1

    cin >> into a std::string reads one whitespace-delimited word.

  2. 2

    The two extractions can be chained.

  3. 3

    Match the sentence wording exactly, including spaces.

Worked solution

Reference solution

Time: O(1) · Space: O(1)

One correct implementation. Any approach that satisfies the constraints is equally valid.

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string name;
  int age;
  if (!(cin >> name >> age)) return 0;
  cout << name << " is " << age << " years old" << '\n';
  return 0;
}

© 2026 ChitChat