Wednesday 09/23/13.

Slides:



Advertisements
Similar presentations
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Advertisements

Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
Chapter 2: Introduction to C++.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 3: Input/Output
Introduction to C++ Programming
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Chapter Two: Fundamental Data Types
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher Fundamental Data Types 09/09/13.
Computer Programming Fundamental Data Types Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Input, Output, and Processing
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
Beginning C++ Through Game Programming, Second Edition
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Chapter Two: Fundamental Data Types
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
Basics (Variables, Assignments, I/O)
Fundamental Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Chapter 2 Assignment and Interactive Input
Chapter 3: Expressions and Interactivity.
Introduction to C++ October 2, 2017.
Basics (Variables, Assignments, I/O)
Fundamental Data Types
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Topics Designing a Program Input, Processing, and Output
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Wednesday 09/23/13

Strings are sequences of characters: "Hello world" If you include the string header, you can create variables to hold literal strings: #include <string> using namespace std; ... string name = "Harry"; // literal string "Harry" stored C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

string response; // literal string "" stored String variables are guaranteed to be initialized even if you don’t initialize them: string response; // literal string "" stored "" is called the empty or null string. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

string name = fname + lname; cout << name << endl; Concatenation Use the + operator to concatenate strings; that is, put them together to yield a longer string. string fname = "Harry"; string lname = "Morgan"; string name = fname + lname; cout << name << endl; name = fname + " " + lname; The output will be HarryMorgan Harry Morgan C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Concatenation of literal strings string greeting = "Hello, " + " World!"; // will not compile Literal strings cannot be concatenated. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

You can read a string from the console: String Input You can read a string from the console: cout << "Please enter your name: "; string name; cin >> name; When a string is read with the >> operator, only one word is placed into the string variable. For example, suppose the user types Harry Morgan as the response to the prompt. This input consists of two words. Only the string "Harry" is placed into the variable name. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

You can use another input to read the second word. String Input You can use another input to read the second word. cout << "Please enter your name: "; string fname, lname; cin >> fname >> lname; gets Harry gets Morgan C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

String Functions The length member function yields the number of characters in a string. Unlike the sqrt or pow function, the length function is invoked with the dot notation: int n = name.length(); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

string greeting = "Hello, World!"; string sub = greeting.substr(0, 5); String Functions Once you have a string, you can extract substrings by using the substr member function. s.substr(start, length) returns a string that is made from the characters in the string s, starting at character start, and containing length characters. (start and length are integer values). string greeting = "Hello, World!"; string sub = greeting.substr(0, 5); // sub contains "Hello" C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

string sub = greeting.substr(0, 5); String Functions 0 ? string sub = greeting.substr(0, 5); C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

0 ? String Functions string greeting = "Hello, World!"; string w = greeting.substr(7, 5); // w contains "World" (not the !) "World" is 5 characters long but… why is 7 the position of the “W” in "World"? 0 ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

String Functions H e l l o , W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11 12 In most computer languages, the starting position 0 means “start at the beginning.” The first position in a string is labeled 0, the second one 1, and so on. And don’t forget to count the space character after the comma—but the quotation marks are not stored. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

H e l l o , W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11 12 String Functions 0 1 2 3 4 5 6 7 8 9 10 11 12 The position number of the last character is always one less than the length of the string The ! is at position 12 in "Hello, World!". The length of "Hello, World!" is 13. (C++ remembers to count the 0 as one of the positions when counting characters in strings.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

If you do not specify how many characters String Functions H e l l o , W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11 12 string greeting = "Hello, World!"; string w = greeting.substr(7); // w contains "World!" If you do not specify how many characters to take, you get all the rest. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

H e l l o , W o r l d ! 0 1 2 3 4 5 6 7 8 9 10 11 12 String Functions 0 1 2 3 4 5 6 7 8 9 10 11 12 string greeting = "Hello, World!"; string w = greeting.substr(); // w contains "Hello World!" If you omit the starting position and the length, you get all the characters (not much of substring!) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

String Operations C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

String Operations C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

String Functions Write this code C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

String Functions ch02/initials.cpp int main() { #include <iostream> #include <string> using namespace std; int main() { cout << "Enter your first name: "; string first; cin >> first; cout << "Enter your significant other's first name: "; string second; cin >> second; string initials = first.substr(0, 1) + "&" + second.substr(0, 1); cout << initials << endl; return 0; } ch02/initials.cpp C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Write variable definitions in C++. Chapter Summary Write variable definitions in C++. • A variable is a storage location with a name. • When defining a variable, you usually specify an initial value. • When defining a variable, you also specify the type of its values. • Use the int type for numbers that cannot have a fractional part. • Use the double type for floating-point numbers. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

• The assignment operator = does not denote mathematical equality. Chapter Summary • An assignment statement stores a new value in a variable, replacing the previously stored value. • The assignment operator = does not denote mathematical equality. • You cannot change the value of a variable that is defined as const. • Use comments to add explanations for humans who read your code. The compiler ignores comments. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Use the arithmetic operations in C ++. Chapter Summary Thank you! Use the arithmetic operations in C ++. • Use * for multiplication and / for division. • The ++ operator adds 1 to a variable; the -- operator subtracts 1. • If both arguments of / are integers, the remainder is discarded. • The % operator computes the remainder of an integer division. • Assigning a floating-point variable to an integer drops the fractional part. • The C++ library defines many mathematical functions such as sqrt (square root) and pow (raising to a power). C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Write programs that read user input and write formatted output. Chapter Summary Write programs that read user input and write formatted output. • Use >>, the extraction operator, to read a value and place it in a variable. • You use manipulators to specify how values should be formatted. Carry out hand calculations when developing an algorithm. • Pick concrete values for a typical situation to use in a hand calculation. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Write programs that process strings. Chapter Summary Write programs that process strings. Strings are sequences of characters Use the + operator to concatenate strings; that is, put them together to yield a longer string. The length member function yields the number of characters in a string. A member function is invoked using the dot notation. Use the substr member function to extract a substring of a string. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Slides by Evan Gallagher End Chapter Two C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher