Your first program
Write, build and run a ten-line C++ program that asks for your name and greets you. Watch it run one line at a time: a variable is made as an empty box in memory, std::cout puts a question on the screen, std::cin reads your answer into the box, and << chains text and the box's value into a greeting. Then see why typing a full name only greets your first name.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01In what order do the statements inside main run?
A program runs main's statements in order, one at a time, from its first line to its last (or until return ends it).
02What does std::string name; do?
It makes a variable: a named box in memory. The type std::string says it holds text; a new std::string is empty ("").
03name holds "Ada". What does std::cout << "Hi, " << name; show?
The pieces are sent left to right: first the fixed text Hi, then the value stored in name. Reading name copies its value; name still holds "Ada".
04The program runs std::cin >> name; and you type Ada Lovelace, then Enter. What is in name?
>> reads one word: it stops at the first space. " Lovelace" is left unread in the input. std::getline(std::cin, name) reads the whole line.
05After running ./greet you type echo $? and see 0. What does the 0 mean?
return 0; ends main and hands the exit status 0 to the system, which means success. echo $? prints the last program's exit status.
Glossary
- Statement
- One instruction in a program, such as std::cin >> name;. Most C++ statements end with a semicolon.
- Variable
- A named box in memory that holds a value, such as name. Its type says what kind of value fits.
- std::string
- The standard library's type for text of any length. A new std::string is empty ("").
- std::cout
- The standard output stream: text sent into it with << appears on the screen (in the terminal).
- std::cin
- The standard input stream: >> reads from it what you type at the keyboard.
- String literal
- Fixed text written in double quotes in the code, such as "Name? ".
- Exit status
- The number a program hands back to the system when it ends. 0 means success; main's return value sets it.
- std::getline
- A function that reads a whole line of input, spaces included, into a std::string.
Notes and sources
- g++ greet.cpp -o greet builds the program in one command (the preprocessor, compiler and linker run in turn). Adding -std=c++20 -Wall -Wextra picks the C++20 rules and turns on helpful warnings; this program builds with no warnings either way. With Clang, type clang++ instead of g++.
- On Windows with Microsoft's compiler, build with cl /EHsc greet.cpp and run greet.exe; the exit status is shown by echo %errorlevel% in Command Prompt, or $LASTEXITCODE in PowerShell.
- In main, and only in main, return 0; may be left out: reaching the closing } of main returns 0 anyway. Writing it makes the success visible.
- The box drawn on the table is a simplification. std::string keeps its characters in memory managed for you; for short text many libraries store them inside the string object itself. When main ends, that memory is given back.
- std::cin >> name first skips any spaces, tabs or new lines, then reads characters up to the next one. The rest stays in the input, where the next read would find it. std::getline reads up to the end of the line instead.
- The question appears before the program waits because std::cin is tied to std::cout: before reading, the output waiting in std::cout is flushed to the screen.
- cppreference.com: Main function
- cppreference.com: std::cout
- cppreference.com: std::cin
- cppreference.com: std::basic_string constructors
- cppreference.com: operator<<, operator>> (std::basic_string)
- cppreference.com: std::getline
- Standard C++ Foundation (isocpp.org): Get Started!
- GCC manual: Options to Request or Suppress Warnings (-Wall, -Wextra)
- GNU Bash manual: Special Parameters ($?)
- Microsoft Learn: Walkthrough: Compiling a Native C++ Program on the Command Line
Quiz Unlocks after the last step.