why does my setprec. affect everything below it?

hi im writing this code to find the average of user input integers but when i find the average which i want to be set to setprecision(1), everything else below it becomes affected, please help me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout << fixed;
    float integers[21];
    float nums;
    float result = 0;
    float average = 0;

    cout << "How many numbers do you want? (max 20)" << endl;
    cin >> nums;

    if (nums > 20)
    {
        cout << "Invalid size.  Ending." << endl;
        return 0;
    }

    for (int counter = 0; counter < nums; counter++)
    {
        cout << "Enter value " << counter << ":" << endl;
        cin >> integers[counter];
        result+=integers[counter];
    }

        average = result / nums;

    cout << "Average: " << setprecision(1) << average << endl;

    cout << "You entered: " << endl;

    for (int x = 0; x < nums; x++)
    {
        cout << integers[x] << endl;
    }

}


this is the program.
with precision() you can get the previous value and reset it after it's done:

http://www.cplusplus.com/reference/ios/ios_base/precision/
It is expected behavior you need to get the precision before setprecision and then reset it afterwards.

Look here for getting the precision http://www.cplusplus.com/reference/ios/ios_base/precision/
Topic archived. No new replies allowed.