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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With