libstdc++/1005: setw() does not work with doubles

klima@iue.tuwien.ac.at klima@iue.tuwien.ac.at
Thu Dec 7 02:26:00 GMT 2000


>Number:         1005
>Category:       libstdc++
>Synopsis:       setw() does not work with doubles
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Dec 07 02:26:00 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     Robert Klima
>Release:        gcc version 2.95.2 19991024 (release)
>Organization:
>Environment:
Redhat Linux 7.0
>Description:
setw() does not work if the following object is a double value:
	cout << setw(20) << 2   << endl; // works
	cout << setw(20) << "2" << endl; // works
	cout << setw(20) << 2.3 << endl; // DOES NOT WORK
The following code does not work properly:
=====================================
#include <iostream>
#include <iomanip>

void main ()
{
   cout << setw(20) << 2.3 << endl;
}
=====================================
Instead of formatting the double value 2.3 right aligned
padding with ' ' setw() fills with invilsible '^@'

This can be observed compiling the code with
	g++ -o file file.cc
and running with (!)
	./file | less
>How-To-Repeat:

>Fix:
#include <stdio.h>
#include <iostream>
#include <iomanip>

void main ()
{
   char buffer[200];
   sprintf(buffer, "%g", 2.3);
   cout << setw(20) << buffer << endl;
}
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the Gcc-bugs mailing list