O'Reilly logo

C++ Primer Plus by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The string Class Input

Another useful thing to know about a class is what input options are available. For C-style strings, recall, you have three options:

char info[100];cin >> info;             // read a wordcin.getline(info, 100);  // read a line, discard \ncin.get(info, 100);      // read a line, leave \n in queue

For string objects, recall, you have two options:

string stuff;cin >> stuff;            // read a wordgetline(cin, stuff);     // read a line, discard \n

Both versions of getline() allow for an optional argument that specifies which character to use to delimit input:

cin.getline(info,100,':');    // read up to :, discard :getline(stuff, ':');          // read up to :, discard :

The main operational difference is that the string ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required