Skip to content

Exercise 2 of 10 · Variables

Name Tag

What you will make

A bordered name-tag card with your own name across three of its lines, every one of them fed by a single line of code.

The one new idea: A variable is a named, typed place to keep a value

Real programs mention the same value again and again — a name, a price, a filename. Storing it under one name means you fix it in one place instead of hunting through every line, and C++ makes you say up front what kind of value that name will ever hold.

Go straight to the code ↓

A named, typed place to keep a value

Every value in a running C++ program lives somewhere, and a variable is how you give that somewhere a name you can use again.

C++
std::string name = "Nina";

Read it in two halves. std::string name declares a variable called name and says, once and for good, what kind of value it will ever hold — a std::string, C++'s type for a run of text. The = "Nina" half gives it a starting value. From here on, every name in the file means whatever that variable is currently holding, not the four letters n-a-m-e.

That type is not decoration. std::string is fixed the moment the variable is declared: this particular name will hold text for the whole of main, and the compiler checks every later use of it against that promise. A Python or JavaScript name can hold a number today and text tomorrow. A C++ variable cannot — and that restriction is what lets the compiler catch a whole category of mistakes before your program ever runs, rather than while a user is watching it fail.

Text versus the name that holds it

One detail decides everything on the three lines that print the card:

C++
std::cout << "name";   // prints the four characters n, a, m, e
std::cout << name;     // prints whatever the variable name is holding

Quotes mean this exact text. No quotes around a word the compiler already knows about means go and look up what that name is holding. That is the entire difference between a card that greets Nina and a card that solemnly welcomes somebody called name.

A worked example

A different variable, so the card in the editor stays yours to finish:

C++
#include <iostream>
#include <string>

int main() {
    std::string team = "Falcons";

    std::cout << "Match day" << std::endl;
    std::cout << "Playing at home: " << team << std::endl;
    std::cout << "Come on the " << team << std::endl;
}
Output
Match day
Playing at home: Falcons
Come on the Falcons

Falcons is typed once, on the first line inside main, and turns up twice below without being typed again. Change that one line to std::string team = "Swifts"; and both lines beneath it follow — they never need editing themselves, because all they ever do is ask what team currently holds.

Your turn

The editor already prints a name-tag card: a border, a heading, and three lines that each read the same variable. Press Run before changing anything, and the card appears with the placeholder still sitting in it.

Find the line near the top:

C++
std::string name = "YOUR NAME";

Replace YOUR NAME with your own name, keep the quotes and the semicolon exactly where they are, and press Run again. Three lines of the card change at once, because all three read that one variable.

If something goes wrong

A red panel instead of a card means the build stopped, not your program. The likeliest cause here is the quotes: single quotes around more than one character will not compile, and the compiler says so plainly — look for no viable conversion from 'int' to 'std::string' and swap the single quotes for double quotes.

If the card prints but one line reads Welcome name instead of your name, that line has quotes around name that should not be there. Take them off and that word becomes a lookup instead of plain text.

Nothing here can break. Change it, run it, change it again.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Putting quotes around name in the std::cout lines
std::cout << "name" prints the four letters n-a-m-e, because quotes mean plain text. Only the value on the std::string line should ever wear quotes — everywhere else, name refers to the variable.
Using single quotes for the value
std::string name = 'Nina'; will not compile. Single quotes hold exactly one character in C++, and Nina is four, so 'Nina' is read as a multi-character integer constant instead of text. The compiler stops with error: no viable conversion from 'int' to 'std::string' — there is no built-in way to turn that number into a std::string.
Declaring name a second time further down
Writing a second std::string name = "..."; below the first is a redeclaration in the same scope, and the compiler refuses to build the program: error: redefinition of 'name'. To change a variable's value later, you assign to it — name = "..."; with no type in front — you do not declare it again.
Misspelling name in one of the std::cout lines, such as Name with a capital N
C++ matches identifiers letter for letter, capitals included, so Name and name are two unrelated things to the compiler. A line asking for Name — which nothing declared — stops the build with error: use of undeclared identifier 'Name'.

Want a blank editor instead? Open the C++ playground.