Topic: URGENT! HELP IN STRING HA
Author: adam.clarke@toronto.can.ipguild.org
Date: Thu, 28 Jul 94 20:58:50 -0500 Raw View
N > If we have a float variable ie a which holds the value lets say
N >how can i create a char pointer which will point to the string "12.34"
N >
Although overkill in this example, I will first reach to ostrstream to
convert a series of data types into strings. For example:
class Foo
{
public:
Foo();
private:
float bar;
public:
GetString(char *string, int maxlen);
}
Foo::Foo()
{
Bar = 12.34;
}
Foo::GetAmount(char *string, int maxlen)
{
ostrstream os(string, maxlen);
os << setw(7) << setprecision(3) << bar << ends;
}
Now in the case of a single float all this work is a bit silly, but if
this was a money class, and you changed the data structure from float to a
long (dollars) and a short (cents) and wanted to add the decimal and
preceed it with a '$' and enclose in brackets if less than zero, this
approach begins to have more appeal.
More flexible still would be to derive your own class from ostream and
overload the insertion operator for your class, this would allow a money
class to be represented differently in a dollars stream from a yen stream
etc..., but that is probably overkill for your immediate need.
---
WinQwk 1.30 #0 Unregistered Evaluation Copy