Re: Doubles and Integers as strings.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"GeorgeJ" wrote:
1) Is there a way, using syntax similar to that Alex used
in his post, e. g.
double f1 = 1.23E2;
cout << setprecision(2) << fixed << f1 << '\n';
cout << scientific << f1 << '\n';
cout << fixed << ' ' << f1 << ' ' << '\n';
to have all that stuff go into a string S1 rather that to
the console, and
then do

cout << S1 ?

Yes, you can make such string by using string streams.
That's why concept of streams was invented in first place:
to separate input/output peculiarities (formats, locales,
etc.) and actual media, which carries bytes, be it file on
disk, string, console or any other device.

In order to make `s1' string from above example, you just
create output string stream:

#include <sstream>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double f1 = 1.23E2;
ostringstream oss;

oss << setprecision(2) << fixed << f1 << '\n';
oss << scientific << f1 << '\n';
oss << fixed << ' ' << f1 << ' ' << '\n';

// Using const reference to avoid
// redundant string copy.
const string& s1 = oss.str();

cout << s1;

return 0;
}

2) where in the help file of Visual Studio 2003 might I
find detailed info
explaining formatted output using cout?

You should have MSDN DVD with VS2003 installation.
Alternatively, you can download MSDN Library directly from
MS site:

"MSDN Library (April 2007 Edition)"
http://www.microsoft.com/downloads/details.aspx?FamilyID=b8704100-0127-4d88-9b5d-896b9b388313&DisplayLang=en

Or just look in MSDN Library online:

"iostream Programming"
http://msdn2.microsoft.com/en-us/library/22z6066f(VS.71).aspx

Alex


.



Relevant Pages

  • Re: wprintf() could not display unicode chars which is > 255 to console?
    ... > console window with formatting strings. ... Unicode and the Windows console is a tricky business. ... string msg, action; ... In Japanese you should have no spaces, and the translator can remove them. ...
    (microsoft.public.vc.mfc)
  • Re: Command Line Interface
    ... This is somewhat I am aiming for, to be honest my idea for this is Java ... Netbean's 'console window' which it uses for user input and output. ... [user enters string and conv to double] ... CString GetString() ...
    (microsoft.public.vc.mfc)
  • Re: UDTs (as was vb6)
    ... That's what the Console Application ... Sub Main ... Public gsClaimFile As String 'if I wanted a global string ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How do you inherit DOS console in Win32 application?
    ... > console in a Win32 assembly console application? ... ByVal lpFile As String, ByVal lpParameters As String, _ ... If your app is calling the command prompt, ... Dim hWnd As Long, iDummy As Long, sCmd As String ...
    (alt.lang.asm)
  • Re: Doubles and Integers as strings.
    ... GeorgeJ wrote: ... to have all that stuff go into a string S1 rather that to the ... console, and then do ... detailed info explaining formatted output using cout? ...
    (microsoft.public.vc.language)