CSE100Lab03 - Lab Section: _ Name: _ Lab 3 getline and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab Section: _____ Name: _________________________________ Lab 3 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. Objectives: In this lab session, you will learn: a when to use the getline function a when to use the cin.ignore operator a what variable casting is and why you would use it a how to format output into columns a how to format data output New Preprocessor Directives: We'll need some new preprocessor directives to complete this lab. In addition to #include <iostream> , we're also going to need #include <string> and #include <iomanip> . The string preprocessor directive includes the code we need to be able to declare string variables, and iomanip gives us more tools we can use to format things we output with cout . Strings: By default, C++ doesn't know how to store or manipulate strings of text (e.g., "Puppies are cute!" ). However, once we use the #include <string> preprocessor definition, we get access to a new data type of string . 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 myString; string yourString = "I think kittens are cuter."; Strings can be assigned new values, and can be shown to the user via cout , like so: yourString = "Just kidding, puppies are awesome. :-)"; cout << yourString << endl; Note that the string type doesn't act exactly like the other types we know ( double , bool , int , etc.). 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. Using + with strings is actually called concatenation , and causes two strings to get stuck together like Legos. For instance, this code snippet: myString = "One plus one equals " + "two!"; gives myString the value "One plus one equals two!" Note that we needed to put that extra space in between "...equals " and "two!" 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 1/9
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Reading Strings Using the getline() Function: Assigning input to a string via cin presents a bit of a problem. Input to cin gets split whenever cin finds whitespace (Tab, Space, Enter, etc.), so trying to input, say, a whole sentence into a string doesn't quite go as planned. For example, if someone typed "Oh what a beautiful morning!" in response to this code: cout << "Say something happy: "; cin >> myString;8 myString would only get the value "Oh" , because cin would stop as soon as it hit the space before "what" . The rest of that happy sentence would stay in the input buffer waiting for someone to take it home and enjoy it. To fix this problem, there's a function called
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/20/2010 for the course CSE 100 taught by Professor Na during the Spring '09 term at Arizona.

Page1 / 9

CSE100Lab03 - Lab Section: _ Name: _ Lab 3 getline and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online