C++: A whole line

Read a line that may contain spaces.

What to build

A program that reads a full line of text.

Requirements

  • Read one whole line.
  • Print You typed: LINE.
  • Print Length: N, the number of characters.

Input

One line of text.

Output

Two lines.

Constraints

The line has from 1 to 200 characters.

Example

Input:

hello world

Output:

You typed: hello world
Length: 11

Why it works

cin >> would stop at the first space, while getline keeps everything up to the end of the line.

Examples

Example 1 · Input
hello world
Expected output
You typed: hello world
Length: 11
Example 2 · Input
a
Expected output
You typed: a
Length: 1

Hints

Hint 1

Use getline(cin, line).

Hint 2

line.size() gives its length.

Solution

Show one correct solution (try it yourself first)
#include <iostream>
#include <string>
using namespace std;

int main() {
    string line;
    getline(cin, line);
    cout << "You typed: " << line << endl;
    cout << "Length: " << line.size() << endl;
    return 0;
}

Lesson: Input and output in depth →