Exercise 1.4
Try to extend the program: (1) Ask the user to enter both a first and last name, and (2) modify the output to write out both names.
We need two strings for our extended program: one to hold the user’s first name and a second to hold the user’s last name. By the end of Chapter 1, we know three ways to support this. We can define two individual string objects:
string first_name, last_name;
We can define an array of two string objects:
string usr_name[ 2 ];
Or we can define a vector of two string objects:
vector<string> usr_name(2);
At this point in the text, arrays and vectors have not yet been introduced, so I’ve chosen to use the two string objects:
#include <iostream> #include <string> using namespace std; int main() { string ...
Get Essential C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.