Adventures in String Input

The strings.cpp program has a blemish that is concealed through the often useful technique of carefully selected sample input. Listing 4.3 removes the veils and shows that string input can be tricky.

Listing 4.3. instr1.cpp

// instr1.cpp -- reading more than one string#include <iostream>int main(){    using namespace std;    const int ArSize = 20;    char name[ArSize];    char dessert[ArSize];    cout << "Enter your name:\n";    cin >> name;    cout << "Enter your favorite dessert:\n";    cin >> dessert;    cout << "I have some delicious " << dessert;    cout << " for you, " << name << ".\n";    return 0;}

The intent of the program in Listing 4.3 is simple: Read a user’s name and favorite dessert from the keyboard and ...

Get C++ Primer Plus 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.