[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: string and strstream and setw
From: |
Paul Serice |
Subject: |
Re: string and strstream and setw |
Date: |
Thu, 22 Feb 2001 06:57:21 GMT |
Maurizio Loreti wrote:
>
> Your example works perfectly with the g++ developement version
> (20010205 snapshot), after:
>
> - having replaced the non-standard header <strtream> with the standard
> one <sstream>;
>
> - having qualified all symbols belonging to namespace std, i.e. having
> changed string to std::string, endl to std::endl, and so on;
>
> - having changed the declaration of a and b to the standard type
> std::ostringstream from the non-standard one ostrstream;
>
> - having changed "cout << a;" to "std::cout << a.str();", assuming you
> want the content of the string stream and not its address in memory;
>
> - having changed in the same way "cout << b;".
Hello,
I just spent all day struggling with a similar problem, but with
std::string. I have the latest version g++ out of Debian unstable
(2.95.3).
Could you give the follow code a quick compile to see if your snapshot
has this fixed?
The output I get is as follows:
Hello, World!Goodbye, Cruel World!
Hello, World! Goodbye, Cruel World!
Thanks
Paul
=====================================
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char* argv[])
{
const string hello = "Hello, World!";
const string goodbye = "Goodbye, Cruel World!";
ios::fmtflags saved_flags = cout.setf(ios::left, ios::adjustfield);
//
// Broken on Debian unstable.
//
cout << setw(40) << hello
<< setw(40) << goodbye
<< endl;
//
// Correct! Use "C"-style strings with cout and setw()!!!
//
cout << setw(40) << hello.c_str()
<< setw(40) << goodbye.c_str()
<< endl;
cout.flags(saved_flags);
return 0;
}