|
-
Jun 3rd, 2002, 07:44 PM
#1
Thread Starter
Fanatic Member
convert strings
anyone know how to convert strings into integers?
strings I meant not chars, I meants strings.
#include <string.h>

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Jun 3rd, 2002, 08:02 PM
#2
simple enough
PHP Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "1234";
int i = atoi(s.c_str());
cout << i << endl;
return 0;
}
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 3rd, 2002, 09:25 PM
#3
Frenzied Member
just for future knowledge the opposite is itoa
-
Jun 3rd, 2002, 09:46 PM
#4
-
Jun 4th, 2002, 06:03 AM
#5
_itoa is a non-standard extension of MS to the CRT. The C standard would be sprintf.
For C++, the standard method would be a stringstream:
Code:
#include <sstream>
#include <string> // no .h, this is old standard and deprecated
#include <iostream>
using namespace std;
int main()
{
// int to string:
ostringstream os;
os << 1249 << " " << 2.34e12;
string s = os.str();
cout << s << endl;
// string to int
istringstream is(s);
int i;
double d;
is >> i >> d;
cout << i << " " << d << endl;
return 0;
}
Does this make you happy parksie?
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.
-
Jun 4th, 2002, 07:47 AM
#6
Monday Morning Lunatic
Yep 
string.h is deprecated in C++ in favour of <cstring>, which is identical apart from placing everything inside namespace std
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
|