Lect6.ppt

advertisement
C++ string Class
Chapter 6
1
Agenda
String Basics (cin, getline ) 
string operations
Table Output using setw()
string I/O using cin & getline()
Functions that take string parameters
2
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
3
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;
4
#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;
5
#include Libraries should be consistent
Pre ’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;
6
string I/O
cin and cout (<< and >> ) work the same
cout<<“Enter two strings”<<endl;
cin>>s1>>s2; // whitespace separates strings
You type
You get
Gong Li
s1
s2
Gong
Li
Problem: How do you get s1 to hold “Gong Li” ???
7
string input with getline( )
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
Gong Li
s2
Quoth the Raven, “Nevermore!”
8
Agenda
String Basics (cin, getline )
string operations 
Table Output using setw()
string I/O using cin & getline()
Functions that take string parameters
9
C++ string operations
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;
10
String quiz
T/F
T/F
T/F
(“Salty” < “Sweet”)
(“aardvark” == “Aardvark”)
(“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( );
_______________
11
Try running name.cpp
Modify to add initial and last_name together
then modify to enter full name using getline()
// name.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
char initial;
string last_name;
cout << "Enter first initial: ";
cin >> initial;
cout << "Enter your last name: ";
cin >> last_name;
cout << last_name << ", " << initial << '.'<< endl;
return 0;
}
12
Agenda
String Basics (cin, getline )
string operations
Table Output using setw() 
string I/O using cin & getline()
Functions that take string parameters
13
#include<iomanip>
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
(ios::left)
Slide 14
// sqrts.cpp
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
Experiment with sqrts.cpp
produce table of numbers
int main()
{
int n;
cout << " N sqrt(N)" << endl;
cout << setiosflags(ios::fixed | ios::showpoint)
<< setprecision(3);
Sets column width
for (n=1; n<=10; n++)
{
cout << setw(2) << n;
cout << setw(8) << sqrt(n);
cout << endl;
}
return 0;
}
15
int main()
{
const double RATE = 4.55;
int num_tutors;
int tutor;
int hours;
string name;
ofstream fout("output.txt");
Experiment with pay.cpp
Produce table of strings
And numbers
cout << "How many tutors worked this week? ";
cin >> num_tutors;
fout << "NAME" << setw(12) << "HOURS" << setw(10)
<< "PAY" << endl;
for (tutor=1; tutor<=num_tutors; tutor++)
{
cout << "Tutor's name and hours worked: ";
cin >> name >> hours;
fout << setiosflags(ios::fixed | ios::showpoint)
<< setprecision(2);
fout << name << setw(16-name.length()) << hours
<< setw(10) << hours*RATE << endl;
}
}
Notice
Complex
setw
Start of Homework Problem 11
16
Agenda
String Basics (cin, getline )
string operations
Table Output using setw()
string I/O using cin & getline() 
Functions that take string parameters
17
Warning—weird behavior mixing
cin>> and getline( )
// It reads no characters for name.
#include <iostream>
#include <string>
using namespace std;
Run nochars.cpp and see what happens
int main()
{
Then see how it’s fixed in name_age.cpp
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";
return 0;
}
18
Agenda
String Basics (cin, getline )
string operations
Table Output using setw()
string I/O using cin & getline()
Functions that take string parameters 
19
Passing strings 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”);
Useful for Prob 6, 7
20
Finally !!! … THE END 
21
Download