7.2. string variables

You can create a variable with type string in the usual ways.

In the active code below, the first line creates a string without giving it a value. The second line assigns it the string value "Hello,". The third line is a combined declaration and assignment, also called an initialization.

Normally when string values like "Hello, " or "world." appear, they are treated as C strings. In this case, when we assign them to an string variable, they are converted automatically to string values.

We can output strings in the usual way:

cout << first << second << endl;

In order to compile this code, you will have to include the header file for the string class, and you will have to add the file string to the list of files you want to compile. The details of how to do this depend on your programming environment.

Run the active code below!

Construct a block of code that correctly prints out a string variable.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

You have attempted 1 of 6 activities on this page