C++ strings

In the previous chapter we briefly mentioned strings and we learned that a string can hold alphanumeric data in anything from a single character to a whole book. We didn't look at declaring, initializing, or manipulating strings. So let's do that now.

Declaring strings

Declaring a string variable is simple. We state the type, followed by the name:

String levelName; 
String playerName; 

Once we have declared a string we can assign a value to it.

Assigning a value to strings

To assign a value to a string, as with regular variables, we simply put the name, followed by the assignment operator, then the value:

levelName = "Dastardly Cave"; 
playerName = "John Carmack"; 

Note that the values need to be enclosed in quotation marks. As with regular ...

Get Beginning C++ Game Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.