Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 need help with changing a double to a string
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
myob




PostPosted: Thu Jan 20, 2005 7:48 pm   Post subject: need help with changing a double to a string

i need help with changing a double variable to a string variable. if anyone knows a command that can change a double variable to a string variable, it will be greatly appreciated
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Jan 20, 2005 8:05 pm   Post subject: (No subject)

C:

code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
   char foo[256];
   double d = 42.27;

   sprintf(foo, "%f", d);

   return 0;
}


C++:

code:
#include <string>
#include <sstream>

int main()
{
   std::stringstream ss;
   double d = 42.27;
   std::string foo;

   ss << d;
   foo = ss.str();

   return 0;
}
myob




PostPosted: Thu Jan 20, 2005 8:35 pm   Post subject: (No subject)

still got a question.. does this work under ccc_win.cpp?

edit:
btw, im using dirty borland c++, dirty school wont let us use visual c++
rizzix




PostPosted: Thu Jan 20, 2005 8:43 pm   Post subject: (No subject)

jeez its the other way around.

borland == clean... vc++ == dirty. gcc == compatibility. gcc is prefered for development.. but borland is prefered for teaching.
wtd




PostPosted: Thu Jan 20, 2005 8:44 pm   Post subject: (No subject)

That code will work with any standards compliant compiler. If it doesn't work with your compiler, then your compiler is deficient.
md




PostPosted: Thu Jan 20, 2005 9:25 pm   Post subject: (No subject)

This also works, and it has the advantage of not using sprintf

code:

#include <stdlib.h>

#define PRECISION 100

...

    char *string
    double d = 10.1010101;
    gcvt(d, PRECISION, string);
...

wtd




PostPosted: Thu Jan 20, 2005 9:29 pm   Post subject: (No subject)

Problem is, that's not standard.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: