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

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Computer Science 1620 Loops.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
The If/Else Statement, Boolean Flags, and Menus Page 180
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm.
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Strings CS303E: Elements of Computers and Programming.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Announcements Exam 1 Tuesday July minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Introduction to C++ Part II Version 1.3. Topics C++ Functions -- passing by value in C++ -- passing by reference in C++ -- passing by address in C++ C++
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
(Dreaded) Quiz 2 Next Monday.
Introduction to Computer Programming
Introduction to C++ (Extensions to C)
CMSC 202 Lesson 2 C++ Primer.
CS 1428 Exam I Review.
Introduction to C++ October 2, 2017.
Quiz Next Monday.
Review for Final Exam.
Counting Loops.
Know for Quiz Everything through Last Week and Lab 7
File Review Declare the File Stream Object Name
Chapter 4: Control Structures I (Selection)
Review for Final Exam.
(Dreaded) Quiz 2 Next Monday.
Today’s Objectives 28-Jun-2006 Announcements
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Programming Strings.
CS 1428 Exam I Review.
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 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 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 #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 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 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 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 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 #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 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 , 274 in Malik

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 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 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 Know for Quiz 2 Everything through Exam 1 Loops: while, for, do-while switch-case Statement New string features Input error checking (cin)