progs/string2.cpp

The following code example is taken from the book
Object-Oriented Programming in C++
by Nicolai M. Josuttis, Wiley, 2002
© Copyright Nicolai M. Josuttis 2002


#include <iostream>    // C++ header file for I/O
#include <string>      // C++ header file for strings

int main()
{
    const std::string c = "input: ";  // string constant
    std::string text;                 // string variable
    std::string s;                    // string variable for the input

    // read string s
    if (! (std::cin >> s)) {
        // read error
        //...
    }

    // compare string with empty string
    if (s == "") {
        // assign string literal to string text
        text = "no input";
    }
    else {
        /* assign string constant c, followed by read string, to text
         */
        text = c + s;
    }
    //...
}