Display Your Name in C++
Store your name in a C++ string variable and print it with cout and endl. Covers #include <string>, chaining <<, and how a string differs from a char array.
1#include <iostream>2#include <string>3using namespace std;45int main() {6 string name = "John Doe";7 cout << "My name is: " << name << endl;8 return 0;9}
Sample Output
My name is: John Doe
The literal label, then the value stored in name, then a newline from endl.
In this one
- How to declare and fill a string variable
- Why #include <string> belongs in the file even if it builds without it
- How to chain several values in one cout statement
- What endl adds to the output
How it works
- 1
Include <iostream> for cout and <string> for the string type.
- 2
Create string name and copy the literal "John Doe" into it.
- 3
cout prints the label "My name is: ".
- 4
The second << prints the contents of name.
- 5
endl ends the line and flushes the output.
Edge cases
Empty string
Change the line to string name = "";
The program still runs and prints My name is: followed by nothing. An empty string is a valid value with length 0, not an error.
Name with quotes inside
Try string name = "John "JD" Doe";
The inner quote ends the literal early and the build fails. Escape it as \" inside the string: "John \"JD\" Doe".
What's going on
This C++ program stores a name in a string and prints it with cout. Same skeleton as Hello World, plus a variable.
#include <iostream>
Needed for cout.
#include <string>
Needed for string. Some compilers pull it in through iostream. Others don't. Include it, or this file builds on one machine and dies on another.
using namespace std;
So string and cout don't need std::.
string name = "John Doe";
A variable holding text. = copies the literal in. Change the quotes, the output changes. This is not cin yet — the name is baked into the source.
cout << "My name is: " << name << endl;
Chain <<. First the label, then the variable, then endl (newline + flush). Forget endl and the next prompt glues onto this line.
Sample: name is "John Doe" → My name is: John Doe.
A C string char name[] = "John Doe"; also prints. string is the one you want for later input and concatenation.
Hard-coding the name is the demo. User Input is the version that reads it with cin.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which header declares the string type?
Pick an answer to check it.
Question 2
What does this print: string s = "Ann"; cout << "Hi " << s;
Pick an answer to check it.
Questions people ask
What is the difference between string and char[]?
string is a class from the standard library that grows, copies, and joins safely. char name[] is a fixed-size C array ending with a hidden \0 character. Both print with cout, but string is easier for input and concatenation.
Why use endl instead of \n?
endl writes a newline and also flushes the output buffer. "\n" only writes the newline. For small programs either is fine; in big loops "\n" is faster.
Can I change the name while the program runs?
Not in this version, because the value is written in the source code. The User Input program reads it from the keyboard instead.