Streamsize, setprecision - Accelerated C++

Could someone explain the following bit of code as I'm still a bit unsure...
I get the jist of it but the following need further explanation (in terms of what they do, what they do in this specific instance and how they work):

streamsize
cout.precision()
setprecision(prec)

1
2
3
4
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
     << 0.2 * midterm + 0.4 * final + 0.4 * sum / count
     << setprecision(prec) << endl;


And why is the above preferable to:
1
2
3
4
5
6
streamsize prec = cout.precision(3);

cout << "Your final grade is "
     << 0.2 * midterm + 0.4 * final + 0.4 * sum / count << endl;

cout.precision(prec) 
1
2
3
4
5
6
7
8
9
10
11
// get the current setting of precision in cout (without changing it)
// and save it in 'prec' for later use 
streamsize prec = cout.precision(); 

// set the precision of cout to 3 and print out the grade with that precision
cout << "Your final grade is " << setprecision(3)
     << 0.2 * midterm + 0.4 * final + 0.4 * sum / count
     
    // and restore precision setting to the old saved value in 'prec'
    // the idea is to leave cout in the same state after our printing is done. 
     << setprecision(prec) << endl;
Topic archived. No new replies allowed.