c++ - why is setw not working? -


#include<fstream.h> #include<iomanip.h> #include<iostream.h> using namespace std; ifstream f("atestat.in"); ofstream g("atestat.out"); int n,i,nr=0; float v[100]; void a(int n) {     for(i=1;i<=n;i++)         f>>v[i];     for(i=1;i<=n;i++)     cout<<v[i]<<" "; } int main() {     f>>n;     a(n);     cout<<endl;     float s=0;     for(i=1;i<=n;i++)     {         if(v[i]<0)         {            s=s+v[i];         nr++;         }     }     cout<<setw(2)<<s/nr<<endl; } 

my "atestat.in" file contains: 6 -56.765 2.3 4.56 -1.2 -1.8 3

the program first displays numbers on second line of "atestat.in" file, through use of array, , supposed display arithmetic mean of negative numbers inside array, precision of 2 numbers after decimal mark. reason, setw(2) nothing @ all, cout<<setw(2)<<s/nr<<endl; displays "19.9217" instead of "19.92"...could tell me why? using improperly somehow?

with precision of 2 numbers after decimal mark

for purpose, need:

std::cout << std::fixed; std::cout << std::setprecision(2) << f << '\n'; //assume f number wanna print 

std::setw not serve purpose:

when used in expression out << setw(n) or in >> setw(n), sets width parameter of stream out or in n.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -