140 likes | 231 Views
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 ( [ ] )
E N D
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 ( [ ] ) • Can use length() object function. • Can add characters to a string.
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); • }
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); • }
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
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;
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
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).
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); • }
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
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); • }
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); • }
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); • }
Know for Quiz 2 • Everything through Exam 1 • Loops: while, for, do-while • switch-case Statement • New string features • Input error checking (cin)