function return ostream with precision

36 views Asked by At

I trying to make an ostream function that return an concatenation of an char and a number. I want the number to be with precision 2: let say "20.1234" so I want to print:"20.12", or if the number is "10" print "10.00". This is my function:

std::ostream& operator<< (std::ostream& out,
        const Assig& assignment)
{
        for (int i=rows-1; i>=0; i--)
        {
            out<<char(assig.arr[i][1])<<" = "<<setprecision(2)<<assig.arr[i][0];
                out<<", ";
        }
    return out;
}

but it print "a = 10" instead of "a =10.00". Maybe I need to use ostringstream? But I can't change the name of the function: "std::ostream& operator<< (std::ostream& out,const Assignment& assignment)"

0

There are 0 answers