|
|
|
|
|
For now, one string operator, concatenation, “+” |
|
Consider |
|
string bookTitle; |
|
string phrase1; string phrase2; |
|
phrase1 = “Programming and ”; |
|
phrase2 = “Problem Solving”; |
|
bookTitle = phrase1 + phrase2; |
|
bookTitle has “Programming and Problem Solving”
stored |
|
bookTitle = phrase2 + phrase1; |
|
bookTitle now has “Problem SolvingProgramming
and ” stored |
|
Warning: At least one of the operands must be a
string variable or named constant. |
|
|
|
|
|
We use cout and the insertion operator
“<<“. |
|
cout << “Hello”; |
|
cout displays the string inserted characters on
the standard output device, usually the video screen. |
|
cout << “The title is ” << bookTitle
+ “ , 2nd Edition”; |
|
OutputStatement |
|
cout << Expression << Expression
…; |
|
|
|
|
Initializations ch = ‘2’ firstName = “Marie” |
|
lastName = “Curie” |
|
Statement What Is Printed |
|
( means blank) |
|
cout << ch; 2 |
|
cout << “ch = ” <<
ch; ch=2 |
|
cout << firstName + “ ” +
lastName; MarieCurie |
|
cout << firstName <<
lastName; MarieCurie |
|
cout << firstName << ‘ ’ <<
lastName; MarieCurie |
|
cout << “ERROR
MESSAGE”; ERRORMESSAGE |
|
cout << “Error=” <<
ch; Error=2 |
|
|
|
|
Program FunctionDefinition |
|
|
|
|
//************************************************************ |
|
// PrintName program |
|
// This program prints a name in two different
formats |
|
//************************************************************ |
|
#include <iostream> |
|
#include <string> |
|
|
|
using namespace std; |
|
|
|
const string FIRST = "Herman"; // Person's first name |
|
const string LAST = "Smith"; // Person's last name |
|
const char
MIDDLE = 'G'; //
Person's middle initial |
|
|
|
|
|
|
|
|
int main() |
|
{ |
|
string
firstLast; // Name in first-last
format |
|
string
lastFirst; // Name in last-first
format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout
<< "Name in first-last format is " << firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout
<< "Name in last-first-initial format is "; |
|
cout
<< lastFirst << MIDDLE << '.' << endl; |
|
|
|
return
0; |
|
} |
|
|
|
|
|
|
int main() |
|
{ |
|
string
firstLast; // Name in first-last
format |
|
string
lastFirst; // Name in last-first
format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout
<< "Name in first-last format is " << firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout
<< "Name in last-first-initial format is "; |
|
cout
<< lastFirst << MIDDLE << '.' << endl; |
|
|
|
return
0; |
|
} |
|
|
|
|
|
|
int main() |
|
{ |
|
string
firstLast; // Name in first-last
format |
|
string
lastFirst; // Name in last-first
format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout
<< "Name in first-last format is " << firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout
<< "Name in last-first-initial format is "; |
|
cout
<< lastFirst << MIDDLE << '.' << endl; |
|
|
|
return
0; |
|
} |
|
|
|
|
|
|
int main() |
|
{ |
|
string
firstLast; // Name in first-last
format |
|
string
lastFirst; // Name in last-first
format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout
<< "Name in first-last format is " << firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout
<< "Name in last-first-initial format is "; |
|
cout
<< lastFirst << MIDDLE << '.' << endl; |
|
|
|
return
0; |
|
} |
|
|
|
|
|
|
int main() |
|
{ |
|
string
firstLast; // Name in first-last
format |
|
string
lastFirst; // Name in last-first
format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout
<< "Name in first-last format is " << firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout
<< "Name in last-first-initial format is "; |
|
cout
<< lastFirst << MIDDLE << '.' << endl; |
|
|
|
return
0; |
|
} |
|
|
|
|
|
|
int main() |
|
{ |
|
string
firstLast; // Name in first-last
format |
|
string
lastFirst; // Name in last-first
format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout
<< "Name in first-last format is " << firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout
<< "Name in last-first-initial format is "; |
|
cout
<< lastFirst << MIDDLE << '.' << endl; |
|
|
|
return
0; |
|
} |
|
|
|
|
|
|
int main() |
|
{ |
|
string
firstLast; // Name in first-last
format |
|
string
lastFirst; // Name in last-first
format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout
<< "Name in first-last format is " << firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout
<< "Name in last-first-initial format is "; |
|
cout
<< lastFirst << MIDDLE << '.' << endl; |
|
|
|
return
0; |
|
} |
|
|
|
|
|
|
int main() |
|
{ |
|
string
firstLast; // Name in first-last
format |
|
string
lastFirst; // Name in last-first
format |
|
|
|
firstLast = FIRST + " " + LAST; |
|
cout
<< "Name in first-last format is " << firstLast |
|
<< endl; |
|
|
|
lastFirst = LAST + ", " + FIRST + ", "; |
|
cout
<< "Name in last-first-initial format is "; |
|
cout
<< lastFirst << MIDDLE << '.' << endl; |
|
|
|
return
0; |
|
} |
|
|
|
|
|
|
|
Statements |
|
cout << “Hi there, ”; |
|
cout << “Lois Lane. ” << endl; |
|
cout << “Have you seen ”; |
|
cout << “Clark Kent?” << endl; |
|
Output |
|
Hi there, Lois Lane. |
|
Have you seen Clark Kent? |
|
|
|
|
|
|
Statements |
|
cout << “Hi there, ” << endl; |
|
cout << “Lois Lane. ” << endl; |
|
cout << “Have you seen ” << endl; |
|
cout << “Clark Kent?” << endl; |
|
Output |
|
Hi there, |
|
Lois Lane. |
|
Have you seen |
|
Clark Kent? |
|
|
|
|
|
|
|
|
Statements |
|
cout << “Hi there, ” << endl; |
|
cout << “Lois Lane. ”; |
|
cout << “Have you seen ” << endl; |
|
cout << “Clark Kent?” << endl; |
|
Output |
|
Hi there, |
|
Lois Lane. Have you seen |
|
Clark Kent? |
|
|
|
|
|
|
Statements |
|
cout << “Hi there, ”; |
|
cout << “Lois Lane. ”; |
|
cout << “Have you seen ”; |
|
cout << “Clark Kent?” << endl; |
|
Output |
|
Hi there, Lois Lane. Have you seen Clark
Kent? |
|
|
|
|
|
|
Problem: You must write a program to write
personalized contest entry letters. As a first effort, you need it to work
for one name. Later, it will read names from a mailing list file. |
|
Output: A form letter with a name inserted at
the appropriate point so that it appears to be a personal letter. |
|
Discussion: You need to insert a name in the
marketing department’s text. The parts of a person’s name need to be stored
separately, different parts are used in different places. You need to use
the string concatenation operator. |
|
|
|
|
|
|
|
//************************************************************ |
|
// FormLetter program |
|
// This program prints a form letter for a
promotional contest. |
|
// It uses the four parts of a name to build
name strings in |
|
// four different formats to be used in
personalizing the letter |
|
//************************************************************** |
|
|
|
#include <iostream> |
|
#include <string> |
|
|
|
using namespace std; |
|
|
|
const string TITLE = "Dr."; // Salutary title |
|
const string FIRST_NAME =
"Margaret"; // First
name of addressee |
|
const string MIDDLE_INITIAL =
"H"; // Middle
initial |
|
const string LAST_NAME =
"Sklaznick"; // Last
name of addressee |
|
|
|
|
int main() |
|
{ |
|
string
first; // Holds the first
name plus a blank |
|
string
fullName; // Complete name,
including title |
|
string
firstLast; // First name and last
name |
|
string
titleLast; // Title followed by
the last name |
|
|
|
//
Create first name with blank |
|
first
= FIRST_NAME + " "; |
|
|
|
//
Create full name |
|
fullName = TITLE + " " + first + MIDDLE_INITIAL; |
|
fullName = fullName + ". " + LAST_NAME; |
|
|
|
//
Create first and last name |
|
firstLast = first + LAST_NAME; |
|
|
|
|
// Create title and last name |
|
titleLast = TITLE + " " + LAST_NAME; |
|
|
|
//
Print the form letter |
|
cout
<< fullName << " is a GRAND PRIZE WINNER!!!!"
<< endl<< endl; |
|
cout
<< "Dear " << titleLast << ","
<< endl << endl; |
|
cout
<< "Yes it's true! " << firstLast << " has
won our" << endl; |
|
cout
<< "GRAND PRIZE -- your choice of a 42-INCH* COLOR"
<< endl; |
|
cout
<< "TV or a FREE WEEKEND IN NEW YORK CITY.**" << endl; |
|
cout
<< "All that you have to do to collect your prize is”<< endl; |
|
cout
<< "attend one fun-filled all-day presentations” << endl; |
|
cout
<< "on the joys of owning a timeshare condominium" <<
endl; |
|
cout
<< "trailer at the Happy Acres Mobile Campground in“ << endl; |
|
cout
<< "beautiful Panhard, Texas!" << first <<
"I know” << endl; |
|
cout
<< "that the 3-hour drive from the nearest airport” << endl; |
|
cout
<< "to Panhard may seem daunting at first, but isn't"
<<endl; |
|
|
|
|
|
|
cout << "it worth some extra
effort to receive such a" << endl; |
|
cout << "FABULOUS PRIZE? So why
wait? Call us right" << endl; |
|
cout << "now to schedule your
visit and collect your" << endl; |
|
cout
<< "GRAND PRIZE!" << endl << endl; |
|
cout
<< "Most Sincerely," << endl << endl; |
|
cout
<< "Argyle M. Sneeze" << endl << endl << endl
<< endl; |
|
cout
<< "* Around the circumference of the packing” << endl; |
|
cout
<< "crate. ** Includes air fare and hotel." << endl; |
|
cout
<< "Departure from Nome, AK; surcharge from” << endl; |
|
cout
<< "other airports. Accommodations within” << endl; |
|
cout
<< "driving distance of New York at the CheapTel” << endl; |
|
cout
<< "in Plattsburgh, NY." << endl; |
|
return
0; |
|
} |
|