1. Consider the following loop for reading a sequence of values:

    double sum = 0;
    double value = 0;
    int count = 0;
    cout << "Enter values, Q to quit: ";
    while (!cin.fail())
    {
       cin >> value;
       sum = sum + value;
       count++;
    }
    cout << sum / count << endl;

    What is printed after the inputs 1 2 3 Q are provided?

    1. 2
    2. 1.5
    3. “Not a number”
    4. The program terminates with an error before sum is printed.

    After the inputs 1 2 3 have been processed, the operation cin >> value fails because the next input is Q and not an integer. value is set to 0, and sum stays unchanged. But count is incremented! The moral is that you should always check for cin.fail() immediately after reading an input.