Strings

5.1 - Creating and inputing strings


There are two kind of strings in C++, std::string and the C-style string (a char array). In C++, std::string is used more often than C-style string since it is more user-friendly.

To use std::string, we need to include the <string> header:

#include <string>

We can create a string just like any other variable:

// Creates a string called my_string.
std::string my_string;

Also just like other variables, we can use std::cin and std::cout with strings:

// Inputs a word from the terminal into my_string.
std::cin >> my_string;

// Prints out my_string.
std::cout << my_string;

Task

Your task is to create a program which takes the user's name and then prints out Hiya <name>!.

Sample input

Tom

Sample output

Hiya Tom!
#include <iostream> #include <string> int main() { }