Strings

From GCHSWiki
Jump to: navigation, search

Storing Strings

You might have noticed that all of the data types so far only allow you to hold really simple values—for example, a single integer or a character. You can actually get quite a bit done with those basics, but C++ also provides other data types.

One of the most useful data types is the string. A string can hold multiple characters. You’ve already seen them used when displaying text on the screen:

cout << "HEY, you, I'm alive! Oh, and Hello World!\n";

But the C++ string class allows you to save, modify and otherwise work with strings. Declaring a string is easy:

#include <string> 
using namespace std; 
int main () 
{ 
    string my_string; 
}

Sample Code 7: string.cpp Notice however, that unlike when you use other built-in types, to use the string, you must use the <string> header file. The reason is that the string type is not built directly into the compiler in the way that integers are. Strings are provided to you by the C++ standard library, a large library of re-usable code.

Just like other basic types provided by C++, you can read in a string from the user using cin.

#include <iostream> 
#include <string> 
using namespace std; 
int main () 
{ 
   string user_name; 
   cout << "Please enter your name: "; 
   cin >> user_name; 
   cout << "Hi " << user_name << "\n"; 
}

Sample Code 8: string_name.cpp This program creates a string variable, prompts the user to enter his or her name, and then prints back out the name.

Just like other variables, strings can be initialized with a value:

string user_name = "<unknown>"; 

If you want to put two strings together, known as appending one string onto another, you can use the + sign:

#include <iostream> 
#include <string> 
using namespace std; 
int main () 
{ 
    string user_first_name; 
    string user_last_name; 
    cout << "Please enter your first name: "; 
    cin >> user_first_name; 
    cout << "Please enter your last name: "; 
    cin >> user_last_name; 
    string user_full_name = user_first_name + " " + user_last_name; 
    cout << "Your name is: " << user_full_name << "\n"; 
}

Sample Code: string_append.cpp This program takes the values of three separate strings, the user's first name, a single space, and the user's last name, and appends them all together into a single value.

When you read in strings, sometimes you want to read a whole line at a time. There is a special function, getline, which can be used to read in the whole line. It will even automatically discard the newline character at the end.

To use getline, you pass in a source of input, in this case cin, the string to read into, and a character on which to terminate input. For example, the following code reads the user's first name:

getline( cin, user_first_name, '\n' );

getline could also be useful if you wanted to read user input only up to another character, such as a comma (the user still has to hit enter before the program will actually accept the data, though): getline( cin, my_string, ',' );

Now if the user types:

Hello, World

The value "Hello" will go into my_string. The rest of the text, in this case, "World", will remain in the input buffer until your program reads it with another input statement.