Archived

This topic is now archived and is closed to further replies.

MetaKnight

STRING to INT

Recommended Posts

Guest Anonymous Poster   
Guest Anonymous Poster
Don''t use char*. Use string.

  
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
string text = "CAMERA10";

//erase everything but numbers

text.erase( remove_if(text.begin(), text.end(), not1(ptr_fun(isdigit))) , text.end());

//prints out "10"

cout << text << endl;

//now lets convert that "10" to a number

int number;
stringstream ss;
ss << text;
ss >> number;

//prints out "10" again. Now ''number'' is 10

cout << number << endl;
}

Share this post


Link to post
Share on other sites
Cybertron    122
the C library function atoi and other string functions, look in MSDN for it

Thats that last thing you need slowing down your program is a string class wasting memory all over the place

Whatever works for you though!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Cybertron, didn''t you notice it''s already the 21st century?

Share this post


Link to post
Share on other sites
Cybertron    122
No I don''t have my flying car yet! Or my robot maid or a space odessy.

I see nothing wrong with the old string functions for what I use them for. Of course if your program is based entirely on string processing I am sure a class is needed

Share this post


Link to post
Share on other sites
Russell    118
quote:
Original post by Anonymous Poster
Cybertron, didn''t you notice it''s already the 21st century?
What does the 21st century have to do with being wasteful? Is being wasteful goin to be more accepted in the future? Don''t think so...

To the original poster, you need to know your data. For example, is it always going to be contiguous letters followed by a number? If so, you could do something like this:
  // untested code

int StripNumber (char * s) {
for (; !isdigit(*s); s++)
;
return atoi(s);
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Russell
What does the 21st century have to do with being wasteful? Is being wasteful goin to be more accepted in the future? Don''t think so...
I call it being at a higher level. Buffer overflows are the cause of many security hazards and bugs in current systems. Most commonly these are caused by primitive string management and too short buffers. People just can''t handle the lower level with the care it requires. For example, the function you gave will make the program crash if the string doesn''t contain any numbers.

Share this post


Link to post
Share on other sites
Russell    118
quote:
Original post by Anonymous Poster
For example, the function you gave will make the program crash if the string doesn''t contain any numbers.

quote:
Original post by Russell For example, is it always going to be contiguous letters followed by a number? If so, you could do something like this

Besides, it depends what you''re doing. You talk about how horrible the security issues are from buffer overflows, and if I''m writing a web server, then maybe I need to think about that, and maybe I should also think about whether I want my web server''s performance to suffer from reallocating memory and copying strings left and right. Since most of us are not writing web servers, this probably isn''t that big of a deal.

Share this post


Link to post
Share on other sites
CaptainJester    523
quote:
Original post by Russell
Besides, it depends what you''re doing. You talk about how horrible the security issues are from buffer overflows, and if I''m writing a web server, then maybe I need to think about that, and maybe I should also think about whether I want my web server''s performance to suffer from reallocating memory and copying strings left and right. Since most of us are not writing web servers, this probably isn''t that big of a deal.


True, but you should always do error checking even when writing the simplest code. This way it becomes a habit and when you work on bigger projects, you get less bug creep.



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share this post


Link to post
Share on other sites