CSE100Lab03 - Lab Section Name Lab 3 getline and cin.ignore...

Info icon This 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
Image of page 1

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

View Full Document Right 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.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern