C++ Primer Plus Chapter 4 Exercise 2

c plus plusExercise 2 requires us to make some minor adjustments to allow for the C++ string class.  By simply deleting any reference to an array and adjusting our getline statement, we have listing 4-4 working for the C++ string class:

2. Rewrite Listing 4.4, using the C++ string class instead of char arrays.

#include <iostream>
#include <string>

using namespace std;

int main()
 {
 string name;
 string dessert;

cout << "Enter your name: ";
 getline(cin, name);

cout << "Enter your favorite dessert: ";
 getline(cin, dessert);

cout << "I have some delicious " << dessert
 << " for you, " << name << endl;

return 0;
 }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s