> lastname; cout << "Enter first name: "; cin >> firstname; cout << "Your name is " << firstname << " " << lastname << endl; return(0); }"> > lastname; cout << "Enter first name: "; cin >> firstname; cout << "Your name is " << firstname << " " << lastname << endl; return(0); }">
Download presentation
Presentation is loading. Please wait.
1
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm
2
2 Strings Revisited string is a type in C++ Library #include 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
3 Reading/Writing Strings #include 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
4 #include 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 Equality Comparison
5
5 On strings, the + operator does concatenation It glues together the two strings to yield a new one. Example: String Concatenation string h = "Hello", w = "world"; string s = h + w; string t = h + ", " + w; cout << s << endl << t << endl; Output: Helloworld Hello, world
6
6 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 Constants & Concatenation string t; t = static_cast ("Hello") + static_cast (", ") + static_cast ("world"); cout << t << endl;
7
7 You can access a character using its index: 0, 1, …. Index of first character is 0, not 1 Example: Accessing Character of a String string s = "alphabet"; cout << s[0] << endl; // prints a cout << s[2] << endl; // prints p cout << s[7] << endl; // prints t
8
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
9 #include 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); } Example with Concatenation and Length
10
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
11 Input Failure Example #include 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
12 Find the Errors #include using namespace std int Main() { // Say Hello 4 times for (i == 0; i < 3; i++) { cout >> "Hello World!" << endl; } return(0); }
13
13 After Fixing the Errors #include using namespace std; int main() { int i; for (i = 0; i < 3; i++) { cout << "Hello World!" << endl; } return(0); }
14
14 Know for Quiz 2 Everything through Exam 1 Loops: while, for, do-while switch-case Statement New string features Input error checking (cin)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.