Beginner · input/output

Say Hello

Everything before this was printing fixed text. Now the output depends on what the user types — which is the moment a program stops being a poster and becomes a tool.

At a glanceRead with <code>cin &gt;&gt; name</code> into a <code>char</code> array, then print <code>Hello, </code> followed by the name and a newline.

Problem

Input: One word of 1 to 20 Latin letters, no spaces.

Output: One line: Hello, NAME! — comma and exclamation mark included, exactly as shown.

Samples:

InputOutput
AdaHello, Ada!
LinusHello, Linus!

The judge compares characters exactly. A missing comma or a missing ! is a wrong answer even though the words are right.

Your solution

main.cpp — your solution
Custom input for "Run my input"

Judging runs in your browser against hidden tests. A runaway loop is cut off after 3 seconds — still, make sure every loop has an exit. Your code is saved in this browser as you type.

Stuck? Hints

Hint 1 — cin reads into a variable

cin >> name; waits for one whitespace-separated token and stores it. The arrow points into the variable — there is nothing to assign afterwards, the read is the assignment.

Hint 2 — Three pieces, one line

Build the output from three pieces: the literal "Hello, ", the variable name, and the literal "!". Chain them with << and finish with endl. Printing name without quotes works because it is a char array, which cout knows how to print as text.

Hint 3 — Why not int?

Because a name is text, not a number. In this engine, text lives in a char array — std::string is not available. Declare it slightly larger than the longest allowed word (25 slots for 20 letters, leaving room for the terminating zero).

Solution (try it first!)
#include <iostream>
using namespace std;

int main() {
    char name[25];
    cin >> name;
    cout << "Hello, " << name << "!" << endl;
    return 0;
}

That is the full input/output loop: read a value, use it, print a result. Everything else on this site is this pattern with more arithmetic in the middle. Next: take two numbers and print their sum in Sum of Two Numbers.

Deep dive

Why this works

The program has exactly the three parts every program has: get input, transform it (here: none), produce output. cin >> name stores the token; cout << "Hello, " << name << "!" assembles the line from literals and the variable. Nothing else is needed.

The cost

O(1) time and space — one token read, one line written, regardless of length within the stated limit.

Where it goes wrong

  • Reading with cin >> name into an int — a name is text, so it needs a char array (or a string type this engine does not have).
  • Forgetting the punctuation. The judge compares characters: Hello, Ada without the ! is a wrong answer.
  • Declaring a too-small array. Twenty letters plus the terminating zero needs at least 21 slots; 25 is comfortable.

Next step

Add arithmetic between the read and the print: Sum of Two Numbers.

Two things to remember

  • Three parts: read, transform, print. This problem has an empty transform.
  • cin >> x reads one whitespace-separated token — the arrow points into the variable.
🐞 This version fails — open it and try

Prints the name twice — the greeting literal is missing

#include <iostream>
using namespace std;

int main() {
    char name[25];
    cin >> name;
    cout << name << name << endl;   // bug: no greeting text
    return 0;
}