Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iomanip with an object of a class

Tags:

c++

In a C++ program I have some code to print an object of a class called fraction. Its variables are n (the numerator), d (the denominator) and sinal (signal: true when a fraction is positive and false otherwise).

ostream &operator << (ostream &os, const fraction &x){//n=0

    if(!x.sinal)
        os << "-";

    os << x.n;

    if(x.d!=1 && x.n!=0)
        os << "/" << x.d;

    return os;
}

It does a good job, but when I try to use a setw() in it, it doesn't work properly: it just affects the first item to be printed (whether it's the signal or the numerator).

I tried to change it and the solution I found was first to convert it to a string and then using the os with a iomanip:

ostream &operator << (ostream &os, const fraction &x){//n=0

    string xd, xn;

    stringstream ssn;
    ssn << x.n;
    ssn >> xn;

    stringstream ssd;
    ssd << x.d;
    ssd >> xd;

    string sfra = "";

    if(!x.sinal)
        sfra += "-";

    sfra += xn;

    if(x.d !=1 && x.n != 0){
        sfra += "/";
        sfra += xd;
    }

    os << setw (7) << left << sfra;

    return os;
}

This works, but obviously I'm not able to change the width that a fraction will have: it will be 7 for all of them. Is there a way to change that? I really need to use different widths for different fractions. Thanks in advance.

like image 205
ahgert Avatar asked May 16 '15 11:05

ahgert


People also ask

Why #include Iomanip is used?

C++ Library - <iomanip> It is used to set basefield flag. It is used to set fill character. It is used to set decimal precision. It is used to set field width.

What does SETW () do in C++?

setw function is a C++ manipulator which stands for set width. The manipulator sets the ios library field width or specifies the minimum number of character positions a variable will consume.

What is the use of SETW function in Iomanip header file explain with coding example?

Setw In C++ Description: Function setw sets the field width or the number of characters to be used for outputting numbers. Example: The setw function is demonstrated using a C++ program. In this program, we print different numbers by setting different values of width.


1 Answers

That's because, as per https://stackoverflow.com/a/1533752 , the width of the output is explicitly reset for each formatted output.

What you could do is at the start of your function get the current width, which is the width set by std::setw (or related), and then set this width explicitly for each value where you want it applied and use std::setw(0) for each value that you want to output as-is:

ostream &operator << (ostream &os, const fraction &x)
{
    std::streamsize width = os.width();

    if(!x.sinal)
        os << std::setw(width) << "-";
    else
        os << std::setw(width); // Not necessary, but for explicitness.

    os << x.n;

    if(x.d!=1 && x.n!=0)
        os << "/" << std::setw(width) << x.d;

    return os;
}

Now you need to improve upon this a bit to handle left and right padding; this only handles left-padding.

like image 87
MicroVirus Avatar answered Oct 29 '22 16:10

MicroVirus