Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Chapter 10.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Introduction to Python
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
C++ fundamentals.
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Working with Strings Lecture 2 Hartmut Kaiser
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Looping and Counting Lecture 3 Hartmut Kaiser
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 02 (Part II) Introduction to C++ Programming.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Chapter 15 - C++ As A "Better C"
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 7: User-Defined Functions II
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Strings (Continued) Chapter 13
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Introduction to C++ Systems Programming.
Introduction to Computer Science / Procedural – 67130
Programming Fundamental
Working with Strings Lecture 2 Hartmut Kaiser
CSC 113: Computer Programming (Theory = 03, Lab = 01)
User-Defined Functions
Chapter 2 part #3 C++ Input / Output
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Console input.
Working with Strings Lecture 2 Hartmut Kaiser
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Variables, Identifiers, Assignments, Input/Output
Wednesday 09/23/13.
Introduction to C++ Programming
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Introduction to Computer Science
Chapter 2 part #3 C++ Input / Output
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Chapter 1 Working with strings

Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations, variables and initialization. Understand basic stream input using the input operator. Get an understanding of some issues related to input and output buffers.

C++ Within C++, there is a much smaller and cleaner language struggling to get out. – Bjarne Stroustrup

Greeting #include int main() { std::cout << "Please enter your first name: "; std::string name; // define name std::cin >> name; // read into name std::cout << "Hello, " << name << "!" << std::endl; system(“pause”); return 0; }

Missing std::endl Notice that the initial output statement does not end with std::endl. This is because we want the input later on in the program to occur at the end of this line.

Variables Unlike some languages (like Python) C++ is typed. In order to store a value we must explicitly create a variable. Technically, a variable is an object that has a name. An object is a part of the computer’s memory that has a type. The type of an object determines how it can be used (its interface) and how much memory must be reserved.

std::string Our program asks for a person’s name which is represented by a string of characters. The type for storing a string of characters ( std::string ) is not included in the core language. We must include the appropriate header file. #include

std::string To create a space in memory to store the string input we define a variable. std::string name; This reserves memory to store a string and associates it with the label name. This is a local variable and will be destroyed when we reach the end of the block } where it is defined.

Initialization When the string is created, it is initialized to a starting value. The programmer can specify the initialization value. If this is not done, the string is initialized to be an empty or null string.

Stream Input Stream input is similar to stream output except that the input operator >> is used with the stream std::cin. This extracts information from the input stream and stores it in the specified variable. When the characters are extracted, leading white space characters (space, tab, backspace, end of line) are removed and characters are extracted up to the first white space encountered after that – the input operator only reads one “word” at a time.

Stream Input Suppose the input is “ Hello, world!” The line std::cin >> name would store “Hello,” in the string. The remaining characters (“ world!”) are still there to be read. Both words could be read by chaining more than one input operator. std::cin >> first >> last;

Buffers It is often more efficient for the computer to store up input and output operations in buffers and do many of them all at once (called flushing the buffer). This can lead to odd behavior. – Output may not occur at the instant the output statement is encountered. – If the input buffer is not empty, additional (unexpected) characters may be in read.

Buffers This odd behavior shows up in two places in our program. – The prompt “Please enter your first name: “ doesn’t appear on the screen when the program encounters the output statement. It actually appears at the input statement. – When the input occurs the white space at the end (end of line character) remains in the input buffer. It will be read (or ignored) by the next input.

Flushing the Output Buffer If we have a program with output but no input we may need to force the output buffer to flush in order to give information to the user. Three events flush the output buffer. – The buffer gets full. – Input is requested from the input stream. – The programmer flushes the buffer. Flushing the buffer, when necessary, is a good habit to get into.

Framed Greeting Suppose we want a more exciting greeting. ******************** * * Hello, Melissa ! * * ********************

Framed Greeting We will build up each line as a string and then print it. First, we input the name as before. Next, we create a string which will contain the greeting we want to frame. const std::string greeting = "Hello, " + name + "!";

Creating Greeting const std::string greeting = "Hello, " + name + "!"; greeting is initialized when it is created. The + operator is used to concatenate std::string variables. – The + operator is overloaded so that it will do one thing when use with numbers and another when used with strings.

+ and Conversions const std::string greeting = "Hello, " + name + "!”; The + operator also converts the string literal "Hello, " into a std::string. – It knows to do this automatic converssion because one of the objects is already a std::string. – It is not legal to concatenate two string literals.

+ is Left Associative const std::string greeting = "Hello, " + name + "!"; There are two + operators in the expressions. Which one gets done first? – The one on the left is done first because + is left associative. – Operators can be either left or right associative.

= is Right Associative We can see right associativity with the = operator. Notice the operation on the right is performed first. Expression x = y = 3 x = (y = 3) x = 3 Side Effect y = 3 x = 3

const Variables const std::string greeting = "Hello, " + name + "!”; const is used in the variable definition. This means greeting cannot be changed later. Why make a variable that cannot be changed? – It protects us from accidentally changing it later. – This is an example of the concept of least privilege. If something shouldn’t happen, make it so it can’t happen.

Strings as Objects To create the lines above and below the greeting we initialize the strings in a different way. Strings are objects and as such they have member functions (methods). Two of these methods are – A constructor. –size() that returns the length of the string.

Building the Frame The second and fourth lines are created using the following. const std::string spaces(greeting.size(), ' '); The syntax for creating an object by invoking its constructor is a little different in C++ than it is in Python, but the idea is the same. The constructor for the string class can accept a number and a character and will then create a string with the character repeated the specified number of times.

Building the Frame Next the ‘*’ characters are added. const std::string second = "* " + spaces + " *"; The first and last lines are created in a similar manner. const std::string first(second.size(), '*'); Finally all the strings are printed in the appropriate order.

Building the Frame std::cout << std::endl; std::cout << first << std::endl; std::cout << second << std::endl; std::cout << "* " << greeting << " *" << std::endl; std::cout << second << std::endl; std::cout << first << std::endl;

Framed Greeting Putting this all together we get the completed program. #include int main() { std::cout << "Please enter your first name: "; std::string name; std::cin >> name; const std::string greeting = "Hello, " + name + "!"; const std::string spaces(greeting.size(), ' '); const std::string second = "* " + spaces + " *"; const std::string first(second.size(), '*');

Framed Greeting std::cout << std::endl; std::cout << first << std::endl; std::cout << second << std::endl; std::cout << "* " << greeting << " *" << std::endl; std::cout << second << std::endl; std::cout << first << std::endl; system(“pause”); return 0; }

Homework Chapter 1 (page 15) Hint, errors can be subtle. Type these in and see what happens. (25 pts total possible) – 1-0 – 1-1 (paper, 5 pts) – 1-2 (paper, 5 pts) – 1-3 (paper, 5 pts) – 1-4 (paper, 5 pts) – 1-5 (paper, 5 pts) – 1-6 (paper, 5 pts)