Re: Doubles and Integers as strings.




--
-regards


"Alex Blekhman" wrote:

"GeorgeJ" wrote:
As a newbie to C++ I would really benefit from the
suggestions of experienced
programmers as to the best to way to convert numbers into
strings and output
these strings to the console. In the various books I've
managed to get ahold
of, various ways are show...

Console::writeln(f)

This is managed C++ (.NET). `Console::writeln' is not
available for native C++ programs.

printf("This is your number %d",f)

This is most commonly used C-legacy method. A lot of people
like it for its simplicity and straightforwardness.

cout << "This is the number" << f

This is intended C++ method to do output (using streams).

One book suggests avoiding the printf command as it can
lead to a program
crash if the type identifier which comes immedeatly after
% doen not
correspond to the type of the variable f.

The book is right. The main problem of `printf'-like
functions is that you can't ensure a validity of parameters
during compile time. Errors are detected only at run time.
It is because `printf' family of functions accept variable
number of argumets, but no information is given about them.
Arguments are checked when format string is parsed.

Suppose I have

double f1=1.23E2;

Is there a way in C++ of using f1 to produce the following
strings

"12300" "1.23E2" " 12300 "

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double f1 = 1.23E2;

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

return 0;
}

int i1=123

" 123"

cout << setw(6) << right << i1 << '\n';

I'd sure appreciate it if someone could show me how these
strings might be
produced and output to the console interspersed with text.

You just infuse text with numbers:

cout << "This is the number:"
<< setw(6) << right << i1 << '\n';

For more info about formatting look here:

"Using Insertion Operators and Controlling Format"
http://msdn2.microsoft.com/en-us/library/420970az(VS.80).aspx

Alex

I must say I'm impressed by the promptness and number of responses. I don't think anyone will be surprised to learn that much of what you folks have posted is way beyond the scope of this newbie's knowledge. Boost()?? imbue() ??

Rather that bugging you folks to explain all that stuff I'm going to
concentrate on Alex's initial (4/14) response to my post. This response was
helpful. I have decided to avoid printf because of the potential crash
problem I mentioned in my post and which Alex confirmed was a danger. My
questions at this point are...

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 ?



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

-Thanks in advance
.



Relevant Pages

  • Re: Doubles and Integers as strings.
    ... programmers as to the best to way to convert numbers into strings and output ... these strings to the console. ... Arguments are checked when format string is parsed. ... produced and output to the console interspersed with text. ...
    (microsoft.public.vc.language)
  • Re: Doubles and Integers as strings.
    ... programmers as to the best to way to convert numbers into ... these strings to the console. ... ....but it's spelled WriteLine, not writeln. ...
    (microsoft.public.vc.language)
  • Re: convert outputs to string,help needed!
    ... Alex wrote: ... > i have many outputs (syms) from one function and these outputs i ... > have to convert into strings. ... > char and num2str is the best? ...
    (comp.soft-sys.matlab)
  • Re: External HTML
    ... some strings - it will build an array and make them scroll vertically along ... website written by Alex to illustrate the principles of design, ... and implementation of a content-based website in ASP.NET. ... This website is a three-tiered solution (Presentation Layer, Business Layer, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: String Concatenation in VB.NET
    ... I second what Alex Says. ... If I've got to concatinate two small strings, ... If I'm building a large string in a loop, ... > string concatenation in VB.NET? ...
    (microsoft.public.dotnet.framework.performance)

Loading