c++ string class chapter 6

23
1 C++ string Class Chapter 6

Upload: barny

Post on 27-Jan-2016

53 views

Category:

Documents


0 download

DESCRIPTION

C++ string Class Chapter 6. Agenda. String Basics (cin, getline )  string operations mixed I/O using >> & getline() Table Output using setw() Functions that take string parameters. What’s a string?. A string is a sequence of letters in quotes - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: C++  string Class Chapter 6

1

C++ string ClassChapter 6

Page 2: 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

Page 3: C++  string Class Chapter 6

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

Page 4: C++  string Class Chapter 6

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;

Page 5: C++  string Class Chapter 6

5

#include <string> 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 <iostream>

#include <string>

using namespace std;

Page 6: C++  string Class Chapter 6

6

#include Libraries should be consistentPre ’98 Standard Post ’98 Standard

<iostream.h>

<fstream.h>

<iomanip.h>

<math.h>

<stdlib.h>

<string> new C++ string

<string.h> old C-string

<iostream>

<fstream>

<iomanip>

<cmath>

<cstdlib>

<string> new C++ string

<cstring> old C-string

You must also add

using namespace std;

Page 7: C++  string Class Chapter 6

7

string I/O

cin and cout (<< and >> ) work the samecout<<“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” ???

Gong Li

Page 8: C++  string Class Chapter 6

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 Li Quoth the Raven, “Nevermore!”

Page 9: C++  string Class Chapter 6

9

Agenda

String Basics (cin, getline )

string operations mixed I/O using >> & getline()

Table Output using setw()

Functions that take string parameters

Page 10: C++  string Class Chapter 6

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;

Page 11: C++  string Class Chapter 6

11

String quizT/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( ); _______________

Page 12: C++  string Class Chapter 6

12

Do Lab5 Problems 1-4

Page 13: C++  string Class Chapter 6

13

Agenda

String Basics (cin, getline )

string operations

mixed I/O using >> & getline() Table Output using setw()

Functions that take string parameters

Page 14: C++  string Class Chapter 6

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

Page 15: C++  string Class Chapter 6

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

Page 16: C++  string Class Chapter 6

16

Agenda

String Basics (cin, getline )

string operations

mixed I/O using >> & getline()

Table Output using setw() Functions that take string parameters

Page 17: C++  string Class Chapter 6

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<iomanip>

Page 18: C++  string Class Chapter 6

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 6Produces a table of square roots

Sets column width

Page 19: C++  string Class Chapter 6

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 7Produce a formatted table of strings and numbers

Page 20: C++  string Class Chapter 6

20

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

Page 21: C++  string Class Chapter 6

21

Agenda

String Basics (cin, getline )

string operations

Table Output using setw()

string I/O using >> & getline()

Functions that take string parameters

Page 22: C++  string Class Chapter 6

22

Passing strings to functionsJust 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”);

Page 23: C++  string Class Chapter 6

23

Finally !!! … THE END