- C++ Game Development By Example
- Siddharth Shekar
- 182字
- 2021-06-24 14:26:10
Strings
Variables that are non-numerical are either a single character or a series of characters called strings. In C++, a series of characters can be stored in a special variable called a string. A string is provided through a standard string class.
To declare and use string objects we have to include the string header file. After #include <conio.h>, also add #include <string> at the top of the file.
A string variable is declared in the same way as other variable types, except before the string type you have to use the std namespace.
If you don't like adding the std:: namespace prefix, you can also add the line using the std namespace. After the #include. This way, you won't have to add the std:: prefix, as the program will understand. However, it can be printed out just like other variables:
#include <iostream> #include <conio.h> #include <string> // Program prints out values to screen int main() { std::string name = "The Dude"; std::cout << "My name is: " << name << std::endl; _getch(); return 0; }
Here is the output: