1
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>

using std::cin; using std::sort;
using std::cout; using std::streamsize;
using std::endl; using std::string;
using std::setprecision; using std::vector;
int main()
{

    cout << "Please enter your midterm and final exam grades: ";
    double midterm, final;
    cin >> midterm >> final;

    cout << "Enter all your homework grades, "
        "followed by end-of-file: ";
    vector<double> homework;
    double x;

    while (cin >> x)
        homework.push_back(x);

        int size = homework.size();
        if (size == 0) {
                    cout << endl << "You must enter your grades. "
                    "Please try again." << endl;
                    return 1;
                        }

    sort(homework.begin(), homework.end());

    int mid = size/2;
    double median;
    median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2
            : homework[mid];

    streamsize prec = cout.precision();
    cout << "Your final grade is " << setprecision(3)
        << 0.2 * midterm + 0.4 * final + 0.4 * median
        << setprecision(prec) << endl;
return 0;
}

Here in this example, what is the point of "streamsize" and why is cout.precision() set up like this? The following lines have "setprecision(3)" and then setprecision(prec) again at the end. Why is that?

streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
    << 0.2 * midterm + 0.4 * final + 0.4 * median
    << setprecision(prec) << endl;
user2826094
  • 133
  • 4

2 Answers2

2

The code resets the precision of std::cout to the original value so that subsequent uses of std::cout don't use three-digit precision.

I would advice using a scope guard, so that the precision will be reset even if an exception is thrown between setprecision(3) and setprecision(prec).

Community
  • 1
  • 1
1

The reason is that the author wanted to set the precision of the value he prints, but preserve the existing precision of the stream for everyone that uses it afterwards.

The reason to use streamsize is simple: It's the exact type return from cout.precision().

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • But what's the point of doing this? That's true, that `cout.precision()` returns `streamsize` type value which is saved in this case into `prec` variable. Later we use this value as input for the function `setprecision(prec)`, but here, `setprecision()` requires not `streamsize` type, but `int` value! – Remis Mar 05 '19 at 08:11
  • @Remis [`setprecision`](https://en.cppreference.com/w/cpp/io/manip/setprecision) is a IO-stream manipulator that behaves as if it you call `str.precision(n);` on the stream. The latter takes `std::streamsize` as its parameter. Why the manipulator uses `int` is not clear to me, it looks like an inconsistency and an oversight. The IO-stream part is one of the oldest parts of the STL, maybe that explains it. Anyways, I feel it should be `std::streamsize` everywhere and that's also why using it to store the value temporarily is the right choice IMHO. – Daniel Frey Mar 09 '19 at 09:00