1 / 14

CS 105 Lecture 8 Strings; Input Failure

CS 105 Lecture 8 Strings; Input Failure. Mon, Mar 7, 2011, 3:39 pm. Strings Revisited. string is a type in C++ Library #include <string> Initialize/assign with double quoted values Can compare using equality operators. Can access single characters using square bracket operator ( [ ] )

nola-watson
Download Presentation

CS 105 Lecture 8 Strings; Input Failure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm

  2. Strings Revisited • string is a type in C++ • Library #include<string> • Initialize/assign with double quoted values • Can compare using equality operators. • Can access single characters using square bracket operator ( [ ] ) • Can use length() object function. • Can add characters to a string.

  3. Reading/Writing Strings • #include <iostream> • #include <string> • using namespace std; • int main() • { • string lastname, firstname; • cout << "Enter last name: "; • cin >> lastname; • cout << "Enter first name: "; • cin >> firstname; • cout << "Your name is " • << firstname << " " • << lastname << endl; • return(0); • }

  4. String Equality Comparison • #include <iostream> • #include <string> • using namespace std; • int main() • { • string lastname, firstname; • bool instructor; • cout << "Enter last name: "; • cin >> lastname; • cout << "Enter Firstname: "; • cin >> firstname; • if (lastname == "Smith") • instructor = true; • else • instructor = false; • return(0); • }

  5. String Concatenation • On strings, the + operator does concatenation • It glues together the two strings to yield a new one. • Example: • string h = "Hello", w = "world"; • string s = h + w; • string t = h + ", " + w; • cout << s << endl << t << endl; • Output: Helloworld Hello, world

  6. String Constants & Concatenation • String constants of the form "..." aren't actually exactly the same thing as strings. • (They're arrays of constant characters — we haven't seen arrays yet.) • If you want to concatenate them, you have to change them using static casting: • string t; • t = static_cast<string>("Hello") • + static_cast<string>(", ") • + static_cast<string>("world"); • cout << t << endl;

  7. Accessing Character of a String • You can access a character using its index: 0, 1, …. • Index of first character is 0, not 1 • Example: • string s = "alphabet"; • cout << s[0] << endl; // prints a • cout << s[2] << endl; // prints p • cout << s[7] << endl; // prints t

  8. Get Length of String • If str is a string, you'd think length(str) would be its length.But it isn't: It's str.length() • Why? The weird syntax is because strings are objects in C++ • Objects combine data and member functions (functions on the data). • The syntax for accessing a member function is stringExpr.fcn(arguments) • The length function takes no parameters (hence no arguments).

  9. Example with Concatenation and Length • #include <iostream> • #include <string> • using namespace std; • int main() • { • string word = "start"; • cout << word << endl; • word[0] = 'h'; • word[1] = 'e'; • cout << word << endl; • cout << "String length is: " << word.length() << endl; • word = word + " " + "healthy" + " " + word; • cout << word << endl; • cout << "String length is: " << word.length() << endl; • return(0); • }

  10. More on Input • Input failure common • Must be part of test plan • cin used for input — if user enters bad type of data, cin left in failed state • Must use cin.clear() function to start over, cin.ignore() function to get beyond bad input • May use cin as condition in if statement • Pages 122-123, 274 in Malik

  11. Input Failure Example • #include <iostream> • #include <string> • using namespace std; • int main() • { • int numEmployees; • bool done = false; • string garbage; • while (!done) • { • cout << "Enter number of employees: "; • cin >> numEmployees; • if (!cin) //if input stream in failed state • { • cout << "Bad input, try again." << endl; • cin.clear(); // changes input state to ok • cin.ignore(200,'\n'); // go beyond bad input chars • } • else • done = true; • } • return(0); • }

  12. Find the Errors • #include <iostream> • #include <string.h> • using namespace std • int Main() • { • // Say Hello 4 times • for (i == 0; i < 3; i++) • { • cout >> "Hello World!" << endl; • } • return(0); • }

  13. After Fixing the Errors • #include <iostream> • #include <string> • using namespace std; • int main() • { • int i; • for (i = 0; i < 3; i++) • { • cout << "Hello World!" << endl; • } • return(0); • }

  14. Know for Quiz 2 • Everything through Exam 1 • Loops: while, for, do-while • switch-case Statement • New string features • Input error checking (cin)

More Related