Lab Section: _____
getline and cin.ignore() functions, variable casting, output formatting
Pre-Lab: Please read the pre-lab and answer the accompanying questions before your lab session.
In this lab session, you will learn:
when to use the getline function
when to use the cin.ignore operator
what variable casting is and why you would use it
how to format output into columns
how to format data output
New Preprocessor Directives:
We'll need some new preprocessor directives to complete this lab.
In addition to
we're also going to need
directive includes the code we need to be able to declare string variables, and
gives us more
tools we can use to format things we output with
By default, C++ doesn't know how to store or manipulate strings of text (e.g.,
"Puppies are cute!"
However, once we use the
preprocessor definition, we get access to a new data type
This type allows us to store strings of text in our program and mess with them as we see fit.
Strings are declared just like normal variables, and can be declared without being initialized or they can
be given an initial value:
string yourString = "I think kittens are cuter.";
Strings can be assigned new values, and can be shown to the user via
, like so:
yourString = "Just kidding, puppies are awesome. :-)";
cout << yourString << endl;
Note that the
type doesn't act
like the other types we know (
Most of the operations we can do with those types like multiplication, type casting, and the like won't
work with strings.
However, one thing that does work (just a little differently) is addition.
strings is actually called
, and causes two strings to get stuck together like Legos.
instance, this code snippet:
myString = "One plus one equals " + "two!";
"One plus one equals two!"
Note that we needed to put that extra space in
When C++ concatenates strings, it doesn't add anything like spaces
between them, so if we want things to be spaced out properly, it's up to us to put the spaces in there.
CSE 100 - Lab 3