1 C++ string Class Chapter 6. 2 Agenda String Basics (cin, getline )  string operations mixed I/O using >> & getline() Table Output using setw() Functions.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Streams In C++ rA stream is a sequence that you either read from or write to. l example – cin is a stream.
Advertisements

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Starting Out with C++, 3 rd Edition 1 Chapter 10 – Characters, Strings, and the string Class.
Getting Data into Your Program
CS31 You Lu CS31-1K TA. Hello World! Variable Out of the Range You input , but it outputs another different number.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Strings & Text File Input CIS Feb-06. Quiz 1.Write a function Prototype for the function swap_values that takes two integers by reference and does.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
©Brooks/Cole, 2001 Chapter 11 Strings. ©Brooks/Cole, 2001 Figure 11-1.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
1 times table 2 times table 3 times table 4 times table 5 times table
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 Chapter 10 Characters, Strings, and the string class.
Exposure C++ Chapter VII Program Input and Output.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
C++ Basics #7 Basic Input and Output. In this video Standard output (cout) Standard input (cin) stringstream.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Characters, Strings, And The string Class Chapter 10.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Chapter 3: Input/Output
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.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Tables Learning Support
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Strings.
Hello Educational presentation.
Example 21 #include<iostream.h> int main() { char Letter = 0;
Topic 2 Input/Output.
Topic Pre-processor cout To output a message.
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
CS 1428 Exam I Review.
Chapter 3: Expressions and Interactivity.
Times Tables.
getline() function with companion ignore()
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 2 – Getting Started
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
Chapter 3: Input/Output
Class Examples.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 3: Expressions and Interactivity
Engineering Problem Solving with C++, Etter
File I/O.
Using string type variables
Strings Skill Area 313 Part C
3 times tables.
6 times tables.
character manipulation
CS 1428 Exam I Review.
getline() function with companion ignore()
CSE Module 1 A Programming Primer
Presentation transcript:

1 C++ string Class Chapter 6

2 Agenda String Basics (cin, getline )  string operations mixed I/O using >> & getline() Table Output using setw() Functions that take string parameters

3 What’s a string? A string is a sequence of letters in quotes “Hello” “C++ is fun!” “” (empty string) cout<<“This is a string literal”<<endl; ofstream fout(“accnts.txt”); this is also a string literal

4 A string variable stores strings string s1; // empty string string s2 = “Hello World”; string s3(60, ‘*’); //s3 contains 60 asterisks s1=s2; //copy string s2 into s1 cout<<“String s1 holds ”<<s1<<endl; cout<<s3<<endl;

5 #include to use string string is a class that was created in ’98 to improve on C-strings (tedious arrays of char) The whole C++ standard was revised as well Keep using these post ’98 libraries : #include using namespace std;

6 #include Libraries should be consistent Pre ’98 StandardPost ’98 Standard new C++ string old C-string new C++ string old C-string You must also add using namespace std;

7 string I/O cin and cout ( > ) work the same cout<<“Enter two strings”<<endl; cin>>s1>>s2; // whitespace separates strings You type Gong Li You get s1 s2 Problem: How do you get s1 to hold “Gong Li” ??? GongLi

8 string input with getline( ) problem 2 getline( ) Reads everything until a ‘\n’ is found getline(cin, s1); getline(cin, s2); You type Gong Li Quoth the Raven, “Nevermore!” You get s1 s2 Gong LiQuoth the Raven, “Nevermore!”

9 Agenda String Basics (cin, getline ) string operations  mixed I/O using >> & getline() Table Output using setw() Functions that take string parameters

10 C++ string operations problem 4 Length of a C++ string can be found as : s.length(); // returns length of s C++ strings can be compared using relational operators like : if(s2 < s5) //… if(name == “Jones”) C++ strings can be concatenated and appended using the + and the += operators : string s6 = s5 + “HIJK”; s2 += s5;

11 String quiz T/F (“Salty” < “Sweet”) T/F(“aardvark” == “Aardvark”) T/F(“John” > “john”) What does full hold? string last=“Woods”, first = “Tiger”, full; a) full = first + last; _______________ b) full = first + “ “ + last; _______________ c) full = last + “, ” + first; _______________ What does k hold? int k = first.length( ); _______________

12 Do Lab5 Problems 1-4

13 Agenda String Basics (cin, getline ) string operations mixed I/O using >> & getline()  Table Output using setw() Functions that take string parameters

14 Warning—weird behavior mixing >> and getline( ) problem 5 string name; int age; cout << "Enter your age: "; cin >> age; cout << "Name (first last): "; getline (cin, name); cout << name << ", you don't look " << age << ".\n"; Enter your age: 43 Name (first last):, you don’t look 43. Console

15 Warning—weird behavior mixing >> and getline( ) problem 5 string name; int age; cout << "Enter your age: "; cin >> age;  does not remove enter key (‘\n’) cout << "Name (first last): "; cin.ignore (80, '\n');  ignores ‘\n’ left by cin>> getline (cin, name); cout << name << ", you don't look " << age << ".\n"; Another option: use >> to read the string or strings (instead of getline) Enter your age: 43 Name (first last):Al Short Al Short, you don’t look 43. Console

16 Agenda String Basics (cin, getline ) string operations mixed I/O using >> & getline() Table Output using setw()  Functions that take string parameters

Slide 17 Creating Space in Output The setw function specifies the number of spaces for the next item Applies only to the next item of output Example: To print the digit 7 in four spaces use outfile<<setw(4)<< 7 << endl; Three of the spaces will be blank 7 7 <<left<< #include

18 int n; cout << " N sqrt(N)" << endl; cout << " " << endl; cout << fixed << showpoint<<setprecision(3); for (n=1; n<=10; n++) { cout << setw(2) << n; cout << setw(8) << sqrt(n); cout << endl; } Application of setw( ) problem 6 Produces a table of square roots Sets column width

19 const double RATE = 4.55; int tutor; int hours; string name; ofstream fout("output.txt"); for (tutor=1; tutor<=3; tutor++) { cout << "Tutor's name and hours worked: "; cin >> name >> hours; fout << fixed << showpoint << setprecision(2); fout << left<< setw(15) << name << right << setw(10)<< hours << setw(10) << hours*RATE << endl; } Notice Left and right justification Application of setw( ) problem 7 Produce a formatted table of strings and numbers

20 Do 5,6,7 (skip 8 for now) and 9

21 Agenda String Basics (cin, getline ) string operations Table Output using setw() string I/O using >> & getline() Functions that take string parameters 

22 Passing string s to functions Just like other parameters, strings can be passed to and returned from functions string AddJunior(string name) { name=name+”, Jr.”; return name; } FUNCTION CALL: string son; son=AddJunior(“Hank Williams”);

23 Finally !!! … THE END