During every iteration in a loop, I need to convert a double value into a
char*. Simple type-casting did not work, so I thought of using
stringstreams. However, the stream would have to be *emptied* after each
iteration (and I could not conceive of a practical way doing that).
Cheers,
Matthias
--
Für emails Anweisung in der Adresse befolgen 16 4851
Der Andere wrote: During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams.
Sounds as if you want a string representation of your double. A char* is
not a string. It's a pointer to char, nothing less, nothing more.
However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
Do the following:
thestream.str("");
Der Andere wrote: During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams. However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
How about using sprintf ?
sprintf(p, "%lf", doubleval);
// Remember to allocate memory to p before you do this.
HTH Cheers, Matthias -- Für emails Anweisung in der Adresse befolgen
--
Karthik
------
Human Beings please 'removeme' for my email.
Rolf Magnus wrote: Der Andere wrote:
During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams.
Sounds as if you want a string representation of your double. A char* is not a string. It's a pointer to char, nothing less, nothing more.
However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
Do the following:
thestream.str("");
Alternatively:
thestream.str( ).clear( );
> > During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams.
Sounds as if you want a string representation of your double. A char* is not a string. It's a pointer to char, nothing less, nothing more.
C-string, yes. I thought I could indicate a c-string with char *: Would
char[] be better? However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
Do the following:
thestream.str("");
It works now, cheers :-)
Matthias
Jeff Schwab wrote: Rolf Magnus wrote: Der Andere wrote:
During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams.
Sounds as if you want a string representation of your double. A char* is not a string. It's a pointer to char, nothing less, nothing more.
However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
Do the following:
thestream.str("");
Alternatively:
thestream.str( ).clear( );
According to Stroustrup, this shouldn't empty the stream, because str()
returns a copy of the string.
Der Andere wrote: > During every iteration in a loop, I need to convert a double value > into a char*. Simple type-casting did not work, so I thought of > using stringstreams.
Sounds as if you want a string representation of your double. A char* is not a string. It's a pointer to char, nothing less, nothing more.
C-string, yes. I thought I could indicate a c-string with char *: Would char[] be better?
No. Actually, char[] is only allowed if used as a paremter. I just
wanted to clarify that char* isn't C's string type. C doesn't have a
string type. Therefore, there is no built-in way to convert something
into a string, and that explains why casting to char* doesn't do what
you want.
On Sun, 25 Apr 2004 16:37:02 -0700, Karthik
<re*******************@yahoo.com> wrote in comp.lang.c++: Der Andere wrote:
During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams. However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that). How about using sprintf ?
That's a possibility but...
sprintf(p, "%lf", doubleval);
In all versions of the ISO C standard prior to 1999, including the
1995 version that the C++ standard inherits from, "%lf" is an
ill-formed conversion specifier for the *printf() functions and
produces undefined behavior.
There is no need for separate conversion specifiers for float and
double in *printf, as floats are always promoted to double as is the
case for all optional parameters to all variadic functions in C and
C++.
The 1999 update to the C standard made the 'l' length modifier a no-op
with the all floating point conversion specifiers, but this is not
part of the current C++ standard. The "%lf" *printf conversion
specifier causes undefined behavior in C++.
// Remember to allocate memory to p before you do this.
HTH Cheers, Matthias
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Jeff Schwab" <je******@comcast.net> wrote in message
news:3o********************@comcast.com... Rolf Magnus wrote: Der Andere wrote:
During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams.
Sounds as if you want a string representation of your double. A char* is not a string. It's a pointer to char, nothing less, nothing more.
However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
Do the following:
thestream.str("");
Alternatively:
thestream.str( ).clear( );
That doesn't work. It clears the error flags of the stream. That's what
clear does for any stream, it has nothing to do with emptying a string
stream.
john
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:sg********************************@4ax.com... On Sun, 25 Apr 2004 16:37:02 -0700, Karthik <re*******************@yahoo.com> wrote in comp.lang.c++:
Der Andere wrote:
During every iteration in a loop, I need to convert a double value
into a char*. Simple type-casting did not work, so I thought of using stringstreams. However, the stream would have to be *emptied* after
each iteration (and I could not conceive of a practical way doing that).
How about using sprintf ?
That's a possibility but...
sprintf(p, "%lf", doubleval);
In all versions of the ISO C standard prior to 1999, including the 1995 version that the C++ standard inherits from, "%lf" is an ill-formed conversion specifier for the *printf() functions and produces undefined behavior.
How did this misunderstanding become so widespread? So widespread in fact
that the C standard committee felt obliged to change the standard.
john
Rolf Magnus <ra******@t-online.de> wrote in message
news:c6*************@news.t-online.com... No. Actually, char[] is only allowed if used as a paremter. I just wanted to clarify that char* isn't C's string type. C doesn't have a string type. Therefore, there is no built-in way to convert something into a string, and that explains why casting to char* doesn't do what you want.
Nor does C++ have a string type. It has a library template instantiation
called. C has a string concept, it is not an intrinsic type just as C++'s is
not. That C string, and you will find the term defined in the C standard, is
an array or array-like region of char terminated by a null character.
To convert a double to such a string, the sprintf() function can be used. I
would recommend using C++ strstreams instead.
Brian Rodenborn
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6************@ID-196037.news.uni-berlin.de... sprintf(p, "%lf", doubleval);
In all versions of the ISO C standard prior to 1999, including the 1995 version that the C++ standard inherits from, "%lf" is an ill-formed conversion specifier for the *printf() functions and produces undefined behavior.
How did this misunderstanding become so widespread? So widespread in fact that the C standard committee felt obliged to change the standard.
Because when you want to read a double value, you use:
sscanf(p, "%lf", &doubleval); // for float: ... "%f", &floatval)
The implicit conversion of variadic function parameters from
float to double does not apply to pointers, as used in scan functions.
Given that the implicit conversion exists in printf (the parameters
received are always double, never float), it kind of makes sense
that %f and %lf be made equivalent, or even that %lf be the primary
choice. (But as Jack pointed out, %lf is formally UB in C++98 and C90).
Regards,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Rolf Magnus wrote: Jeff Schwab wrote:
Rolf Magnus wrote:
Der Andere wrote: During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams.
Sounds as if you want a string representation of your double. A char* is not a string. It's a pointer to char, nothing less, nothing more. However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
Do the following:
thestream.str("");
Alternatively:
thestream.str( ).clear( );
According to Stroustrup, this shouldn't empty the stream, because str() returns a copy of the string.
Whoops! You're absolutely right.
John Harrison wrote: "Jeff Schwab" <je******@comcast.net> wrote in message news:3o********************@comcast.com...
Rolf Magnus wrote:
Der Andere wrote: During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams.
Sounds as if you want a string representation of your double. A char* is not a string. It's a pointer to char, nothing less, nothing more. However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
Do the following:
thestream.str("");
Alternatively:
thestream.str( ).clear( );
That doesn't work. It clears the error flags of the stream. That's what clear does for any stream, it has nothing to do with emptying a string stream.
No, John, you've mistaken the clear() method of the stream for the
clear() method of the underlying stream. The str() method here returns
a string, not a stream.
This is a great example of why unnecessary abbreviations are such a bad
idea when choosing identifiers.
John Harrison wrote: "Jeff Schwab" <je******@comcast.net> wrote in message news:3o********************@comcast.com...
Rolf Magnus wrote:
Der Andere wrote: During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams.
Sounds as if you want a string representation of your double. A char* is not a string. It's a pointer to char, nothing less, nothing more. However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
Do the following:
thestream.str("");
Alternatively:
thestream.str( ).clear( );
That doesn't work. It clears the error flags of the stream. That's what clear does for any stream, it has nothing to do with emptying a string stream.
john
No, John, you're wrong. You've mistaken the clear() method of the
stream for the clear() method of the underlying stream. The str()
method here returns a string, not a stream. This does clear the string
representation of the stream's contents, not the stream's error flags.
(Unfortunately, as Rolf pointed out, it clears only a temporary copy of
the string, so the stream is unaffected. I wonder, why isnt the
temporary string const?)
This is a great example of why unnecessary abbreviations are such a bad
idea when choosing identifiers.
On Mon, 26 Apr 2004 06:13:44 +0100, "John Harrison"
<jo*************@hotmail.com> wrote in comp.lang.c++: "Jack Klein" <ja*******@spamcop.net> wrote in message news:sg********************************@4ax.com... On Sun, 25 Apr 2004 16:37:02 -0700, Karthik <re*******************@yahoo.com> wrote in comp.lang.c++:
Der Andere wrote:
> During every iteration in a loop, I need to convert a double value into a > char*. Simple type-casting did not work, so I thought of using > stringstreams. However, the stream would have to be *emptied* after each > iteration (and I could not conceive of a practical way doing that).
How about using sprintf ?
That's a possibility but...
sprintf(p, "%lf", doubleval);
In all versions of the ISO C standard prior to 1999, including the 1995 version that the C++ standard inherits from, "%lf" is an ill-formed conversion specifier for the *printf() functions and produces undefined behavior.
How did this misunderstanding become so widespread? So widespread in fact that the C standard committee felt obliged to change the standard.
john
How did "void main()" become so widespread, as it is considerably more
so than "%lf"?
The real problem is the lack of symmetry between the *printf and
*scanf families. Both "%f" and "%lf" are required for the *scanf
family of functions, which receive a pointer to the destination type
and therefore can convert to both float and double.
Given that, it would have been better to accept "%lf" in printf all
along, and many compilers do as an extension, but there are also some
(no idea on relative percentage) that choke on it, as the standard
entitles them to do.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Der Andere" <ma**************@gmx.net> wrote in message news:<c6*************@news.t-online.com>... During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams. However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).
I'm not sure you need to "empty" the stream after each iteration,
maybe just construct a new one.
Maybe like this
----------------------------
#include <iostream>
#include <sstream>
int main() {
static const double x[] = { 0.5, 123.456789, -99.2 };
for(int i=0; i<sizeof(x)/sizeof(x[0]); i++) {
std::ostringstream ox;
ox << x[i];
std::cout
<< i << " " // number of item
<< x[i] << " " // raw item
<< ox.str() << " " // item as string
<< ox.str().c_str() // item as char *
<< std::endl;
}
return 0;
}
----------------------------
The output is:
0 0.5 0.5 0.5
1 123.457 123.457 123.457
2 -99.2 -99.2 -99.2
HTH
LR This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pepijn Kenter |
last post by:
Dear experts.
I have a vector<float> and want to convert that to a vector<double>. I
optimistically tried:
#include <vector>
#include <iostream>
using namespace std;
int main() {
|
by: Anonymous |
last post by:
Is there a non-brute force method of doing this?
transform() looked likely but had no predefined function object.
std::vector<double> src;
std::vector<int> dest;
...
|
by: Bjørn Augestad |
last post by:
Below is a program which converts a double to an integer in two
different ways, giving me two different values for the int. The basic
expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
|
by: npotnis |
last post by:
Hi All,
I am trying to compile an existing C++ unamanaged application with the /clr switch in VS.Net 2003, as I need to use managed extensions in the code.
In a cpp file I have the following...
|
by: Bo Peng |
last post by:
Dear C++ experts,
I need to store and retrieve a meta information that can be int or
double. The program would be significantly simpler if I can handle two
types uniformly. Right now, I am using...
|
by: Ole Nielsby |
last post by:
First, sorry if this is off-topic, not strictly being a C++ issue.
I could not find a ng on numerics or serialization and I figure
this ng is the closest I can get.
Now the question:
I want...
|
by: J.M. |
last post by:
I have data in a double array of length 2N, which actually represents
complex numbers with real and imaginary parts interlaced. In other words,
elements in this array with even indices represents...
|
by: perroe |
last post by:
Hi
I have a array of complex numbers that are stored in a simple double
array. This is done since the array is part of an wrapper for an
external C library, and the imaginary part of the first...
|
by: Steven Woody |
last post by:
long i = nnn;
long j;
double d;
d = i;
j = ( long )d;
in this case, i == j ?
thanks.
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |