is there a way to convert numbers to strings ?
the number could be integer/float ..
www.akilla.tk
This is a discussion on convert numbers to strings within the C++ Programming forums, part of the General Programming Boards category; is there a way to convert numbers to strings ? the number could be integer/float .. www.akilla.tk...
is there a way to convert numbers to strings ?
the number could be integer/float ..
www.akilla.tk
for int
char *_itoa( int value, char *string, int radix );
for float
char *_fcvt( double value, int count, int *dec, int *sign )
look um up.
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.
The format specifiers are the same as for printf.Code:char word[20]; int number1 = 12345; float number2 = 678.9; sprintf(word, "%d %f", number1, number2); cout << word;
the output is
12345 678.900024
here's my code...
COOL PROGRAMS @ www.akilla.tkPHP Code:#include <stdio.h>
#include <iostream.h>
main()
{
char word[20];
int number1 = 12345;
float number2 = 678.9;
sprintf(word, "%d %f", number1, number2);
cout << word;
cin.get();
}
By the way, how do you know about these functions ??for int
char *_itoa( int value, char *string, int radix );
for float
char *_fcvt( double value, int count, int *dec, int *sign )
look um up.
Is there a book that tells all these things that I can refer
to ? Is it online ?
COOL PROGRAMS @ www.akilla.tk
That's the disadvantage of using floats.Originally posted by Unregistered
the output is
12345 678.900024
Your program is good (you could try: %.1f) but the OS can't handle floats very well.
You can take a look in the Microsoft MSDN library for example: _itoaOriginally posted by Unregistered
By the way, how do you know about these functions ??
Is there a book that tells all these things that I can refer
to ? Is it online ?
>
By the way, how do you know about these functions ??
Is there a book that tells all these things that I can refer
to ?
<
butt loads of questions, and research such as reading my headers, and the MSDN as Monster posted, it defines and explains virtually every function in standard C/C++ and many other languages.
it is Microsofts single greatest accomplishment.
>Is it online ?
yeah,
http://msdn.microsoft.com
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.
I like this one as well:
On my machine (Solaris CC compiler) I don't have that pesky formatting problem.Code:#include <sstream> using namespace std; int main() { ostringstream oss, oss2; oss << 12345; oss2 << 678.9; string s1 = oss1.str(), s2 = oss2.str(); cout << s1 << endl << s2 << endl; }
Claus Hetzer
Compiler: Borland 5.5 (on Windows)
Solaris CC (on Unix)
Known Languages: C++, MATLAB, Perl, Java
Perhaps you could write an algorithm to do it? It is not as difficult as you may think...
Code:int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000 <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000 )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}