More C++ Statements

Look at a couple more examples of statements. The program in Listing 2.3 expands on the preceding example by allowing you to enter a value while the program is running. To do so, it uses cin (pronounced cee-in), the input counterpart to cout. Also, the program shows yet another way to use that master of versatility, the cout object.

Listing 2.3. yourcat.cpp
// yourcat.cpp -- input and output
#include <iostream>
using namespace std;
int main()
{
    int fleas;

    cout << "How many fleas does your cat have?\n";
    cin >> fleas;               // C++ input
// next line concatenates output
    cout << "Well, that's " << fleas << " fleas too many!\n";

    return 0;
}

Here is a sample output:

How many fleas does your cat have?
112 Well, that's 112 fleas too ...

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