|
-
Sep 25th, 2002, 07:24 PM
#1
Thread Starter
Frenzied Member
convert double to string (string.h string)
Im having a bit of trouble with conversions to this string thing (from string.h). Is there an all knowing conversion table or something of the like?
PHP Code:
string s; double d; d = 3.14 s = string(d); cout << s << endl;
there isnt a constructor with a double argument...
retired member. Thanks for everything 
-
Sep 25th, 2002, 10:41 PM
#2
Addicted Member
Hi,
I just got this really cool summary of converting numbers to strings and vice versa from a German forum http://www.c-plusplus.de...submitted by the C++ moderator. Look beyond the German and you can read the code.
Code:
a) Zahl nach String: (numbers to string)
1. std::ostringstream
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
double d = 3.14;
ostringstream Str;
Str << d;
string ZahlAlsString(Str.str());
cout << ZahlAlsString << endl;
}
2. std::ostrstream
#include <strstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
double d = 3.14;
char buffer[10];
ostrstream Str(buffer, 10);
Str << d << ends;
string ZahlAlsString(Str.str());
cout << ZahlAlsString << endl;
}
3. sprintf
#include <stdio.h>
#include <stdlib.h>
int main()
{
double d = 3.14;
char buffer[10];
sprintf(buffer, "%g", d);
printf("%s\n", buffer);
system("pause");
}
b) String nach Zahl (string to number)
1. std::stringstream
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string AlsString("3.14");
stringstream Str;
Str << AlsString;
double d;
Str >> d;
cout << d << endl;
}
2. std::strstream
#include <strstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string AlsString("3.14");
char buffer[10];
strstream Str(buffer, 10);
Str << AlsString << ends;
double d;
Str >> d;
cout << d << endl;
}
3. atof, atoi, atol
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
const char* AlsString = "3.14";
double d = atof(AlsString);
cout << d << endl;
}
4. sscanf
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
const char* AlsString = "3.14";
float d = 0;
// ACHTUNG! sscanf erwartet ein float. Kein double!
sscanf(AlsString, "%g", &d);
cout << d << endl;
}
5. strtod, strtol, strtoul
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string AlsString("3.145");
char* StopPosition = 0;
double d = strtod(AlsString.c_str(), &StopPosition);
cout << d << endl;
}
Regards,
ChuckB
-
Sep 26th, 2002, 02:27 AM
#3
Frenzied Member
string streams are pretty nice =)...
Code:
strstream sstream;
sstream << "10" << endl;
sstream << 20 << endl;
string s;
long l;
sstream >> l
sstream >> s
cout << sstream.str() << endl << endl;
cout << l << endl;
cout << s << endl;
Z.
-
Sep 26th, 2002, 06:45 AM
#4
Monday Morning Lunatic
Zaei - use stringstreams, not strstreams 
Oh, quick function for you:
Code:
template <typename T>
string toString(const T &ref) {
ostringstream oss;
oss << ref;
return oss.str();
}
...or something
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 26th, 2002, 10:18 AM
#5
Frenzied Member
Forgive me =). It was late =).
Z.
-
Sep 26th, 2002, 06:32 PM
#6
Thread Starter
Frenzied Member
Perfect everyone 
Templates work in g++? Ill assume yes; otherwise reply
retired member. Thanks for everything 
-
Sep 26th, 2002, 06:37 PM
#7
Monday Morning Lunatic
Well, if you use the 3-series, then yes.
3.2 is the latest I think...but it can't compile the kernel. Oops. Need 2.95.3 for that
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 26th, 2002, 06:44 PM
#8
Thread Starter
Frenzied Member
then *** is it a new version if it cant do that?
retired member. Thanks for everything 
-
Sep 27th, 2002, 05:25 AM
#9
There may be some dropped extensions that the kernel used, this would explain why it doesn't work anymore.
Another explanation would be that it's simply a bug.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2002, 05:59 AM
#10
Monday Morning Lunatic
Most people point at the second. Linus recommends 2.95.3, and I haven't been able to compile the 2.5.38 kernel with anything else!
For most normal code, I use 3.2, because it's C++ support is far better.
2.95 can do templates, but it doesn't support the more esoteric features.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 06:31 AM
#11
Why do you need the kernel recompiled?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2002, 06:36 AM
#12
Monday Morning Lunatic
The 2.5 series is the current development version.
/me is just playing 
I built 2.4.19 on my other disk as part of a test (trying to put something minimalist together).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 08:31 AM
#13
Frenzied Member
Out of curiosity, is there any Linux IDE/Code Editor running around out there that has vim style code indentation?
Example:
Code:
int main()
{
>>>>code goes over here when I hit enter
>>>>same thing
} <<<< curley brace comes back over here, without me hitting that backspace key
vim, and MSVC are the only editors that I have seen that do this...
Z.
-
Sep 27th, 2002, 08:35 AM
#14
Monday Morning Lunatic
nedit and emacs can do it.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 08:40 AM
#15
Frenzied Member
Figured emacs could do it =).
Z.
-
Sep 27th, 2002, 08:43 AM
#16
Monday Morning Lunatic
It does everything.
If you've got it, at the console type
Code:
$ emacs -batch -l dunnet
Enjoy! 
(found that one at work...my productivity dropped for a couple of weeks )
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 03:15 PM
#17
Thread Starter
Frenzied Member
Ok Im having a bit of trouble using the template. I cant make a prototype that works. They all stem massive ammounts of errors.
retired member. Thanks for everything 
-
Sep 27th, 2002, 03:18 PM
#18
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 04:19 PM
#19
Edit++ in Windows can do it too...
But you probably won't get it running, not even under WINE, as it uses MFC and all kinds of weird windows features such as OLE drag/drop, embedded IE etc.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2002, 04:22 PM
#20
And while we're at gcc...
Anyone know where I can get a x86 binary of gcc 3.x or any C compiler that can compile gcc 3.2 for RedHat? I have a RedHat-derived server distro called e-smith (now SME server), and it doesn't come with any C compiler. Only as and ld (so if you got assembler source for any compiler it's ok too ).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2002, 04:28 PM
#21
Monday Morning Lunatic
Can't you just install the 2.95.3 binary RPM? That'll let you build 3.2.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 04:30 PM
#22
Hmm, gotta search again, last time I looked I was sure I didn't find any RPM for gcc.
I'll look for it tomorrow.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2002, 04:34 PM
#23
Monday Morning Lunatic
There must be one, it needs to come with RedHat
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 04:37 PM
#24
Yet that doesn't mean it's available in public.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 27th, 2002, 04:55 PM
#25
Monday Morning Lunatic
I don't follow. It must be available, almost by definition.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 05:37 PM
#26
Frenzied Member
Originally posted by parksie
It does everything.
If you've got it, at the console type
Code:
$ emacs -batch -l dunnet
Enjoy! 
(found that one at work...my productivity dropped for a couple of weeks )
ROFL =). Thats pretty good =). Now, where is the emacs fridge? =).
Z.
-
Sep 27th, 2002, 05:44 PM
#27
Monday Morning Lunatic
-
Sep 27th, 2002, 05:58 PM
#28
Frenzied Member
I feel a whole lot more comfortable in vim, but emacs seems a LOT more powerful... If im doing some coding, ill open two telnet sessions... leave one in vim, and use the other for compiling.. saves a lot of back and forth.
And who could think of NOT doing it without the mouse... I mean... what is that? =).
Z.
-
Sep 27th, 2002, 06:07 PM
#29
Monday Morning Lunatic
A lot more powerful? It has everything bar the kitchen sink. No, wait, that's in there 
It even has keyboard shortcuts for moving around so you don't have to move from the touch-typing position to the arrow keys Quality stuff
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 06:14 PM
#30
Frenzied Member
Its like an operating system... =).
Z.
-
Sep 27th, 2002, 06:21 PM
#31
Monday Morning Lunatic
Pretty much, yeah, only it has more features You can do most of what you'd often do from inside it, mail, news, programming.
Mostly everything except browse the web (although who knows, maybe...)
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 06:27 PM
#32
Frenzied Member
lynx.. im sure you can use lynx from it... =).
Z.
-
Sep 27th, 2002, 06:30 PM
#33
Monday Morning Lunatic
/me tries if he can remember those damn key combos
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2002, 11:03 PM
#34
Thread Starter
Frenzied Member
Originally posted by parksie
Zaei - use stringstreams, not strstreams 
Oh, quick function for you:
Code:
template <typename T>
string toString(const T &ref) {
ostringstream oss;
oss << ref;
return oss.str();
}
...or something
that.
retired member. Thanks for everything 
-
Sep 28th, 2002, 05:18 AM
#35
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 28th, 2002, 07:12 AM
#36
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 28th, 2002, 11:11 AM
#37
Thread Starter
Frenzied Member
This is the code that doesnt work without a prototype. What prototype should be used for this?
PHP Code:
template <typename T>
string toString(const T &ref) {
ostringstream oss;
oss << ref;
return oss.str();
}
retired member. Thanks for everything 
-
Sep 28th, 2002, 03:22 PM
#38
template <typename T>
std::string toString(const T& ref);
BTW I would inline this one.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 28th, 2002, 05:10 PM
#39
Thread Starter
Frenzied Member
not working
PHP Code:
#include <iostream> #include <string> using namespace std;
template <typename T> std::string toString(T &ref);
int main() { double d = 3.14; string s; s = toString(d); cout << s << endl;
}
template <typename T> std::string toString(T &ref) { ostringstream oss; oss << ref return oss.str(); }
g++ -Wall test.cpp yields:
test.cpp: In function `int main ()':
test.cpp:10: universal-character-name `\u00a0' not valid in identifier
test.cpp:10: universal-character-name `\u00a0' not valid in identifier
test.cpp:10: sorry, not implemented: universal characters in
identifiers
test.cpp:10: universal-character-name `\u00a0' not valid in identifier
test.cpp:10: sorry, not implemented: universal characters in
identifiers
test.cpp:10: universal-character-name `\u00a0' not valid in identifier
test.cpp:10: sorry, not implemented: universal characters in
identifiers
test.cpp:10: `___double' undeclared (first use this function)
test.cpp:10: (Each undeclared identifier is reported only once for each
function it appears in.)
test.cpp:10: parse error before `='
test.cpp:12: `d' undeclared (first use this function)
test.cpp: In function `string toString (T &)':
test.cpp:20: universal-character-name `\u00a0' not valid in identifier
test.cpp:20: universal-character-name `\u00a0' not valid in identifier
test.cpp:20: sorry, not implemented: universal characters in
identifiers
test.cpp:20: universal-character-name `\u00a0' not valid in identifier
test.cpp:20: sorry, not implemented: universal characters in
identifiers
test.cpp:20: universal-character-name `\u00a0' not valid in identifier
test.cpp:20: sorry, not implemented: universal characters in
identifiers
test.cpp:20: parse error before `;'
test.cpp:22: parse error before `return'
retired member. Thanks for everything 
-
Sep 28th, 2002, 05:22 PM
#40
Monday Morning Lunatic
I think something messed up when you typed it in. It's complaining at the tokenising level that you've got some bad characters in there.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|