Small Steps I — Creating with Text and Numbers

Asking for a name while running

Read a name and print it after Nice to meet you,.

What you will learn

input means receiving a value from outside the program. The input function returns a line typed on the keyboard as a string.

Try it

Example · example.cpp
void small_main()
{
    String name = input();
    print("Hello, ", name);
}

After Run, type Alex in the console and press Enter. The program is not broken while input waits. When you finish, it stores the string in name and runs the next line.

You can also put a prompt inside the parentheses, as in input("Name: ").

Expected output (excluding input prompts):

Hello, Alex

Exercise

Read a name and print it after Nice to meet you,.

Exercise starter · exercise_starter.cpp
void small_main()
{
    String name = input();
    print("Hello, ", name);
}

Hint

Change the parts of the example that you need.

show solution
void small_main()
{
    String name = input();
    print("Nice to meet you, ", name);
}
← Previous lessonNext lesson →