summaryrefslogtreecommitdiff
path: root/chapter4/str_demo.cxx
blob: df7877e84f179adcc6eaff6b0c4a36c0d9a204f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// FILE: str_demo.cxx
// This is a small demonstration program showing how the string class is used.
#include <iostream>     // Provides cout and cin
#include <cstdlib>      // Provides EXIT_SUCCESS
#include "mystring.h"   // Or use the Standard Library <string>
using namespace std;
using namespace main_savitch_4;

// PROTOTYPES for functions used by this demonstration program:
void match(const string& variety, const string& mine, const string& yours);
// The two strings, mine and yours, are compared. If they are the same, then a 
// message is printed saying they are the same; otherwise mine is printed
// in a message. In either case, the string variety is part of the message.

int main( )
{
    const string BLANK(" ");
    string me_first("Demo"), me_last("Program");
    string you_first, you_last, you;
    
    cout << "What is your first name? ";
    cin >> you_first;
    match("first name", me_first, you_first);
    cout << "What is your last name? ";
    cin >> you_last;
    match("last name", me_last, you_last);

    you = you_first + BLANK + you_last;
    cout << "I am happy to meet you, " << you << "." << endl;
    return EXIT_SUCCESS; 
}

void match(const string& variety, const string& mine, const string& yours)
{
    if (mine == yours)
        cout << "That is the same as my " << variety << "!" << endl;
    else
        cout << "My " << variety << " is " << mine << "." << endl;
}