I wish to convert a string to an int. The string may contain digits (0-9), a negative sign (-), or a decimal point (.). Is there a function to handle this? Can you be quickly written? Btw, I can either use c-style char * strings, or STL strings.
I wish to convert a string to an int. The string may contain digits (0-9), a negative sign (-), or a decimal point (.). Is there a function to handle this? Can you be quickly written? Btw, I can either use c-style char * strings, or STL strings.
atoi()
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
atoi() for null terminated strings.
toInt() or some such function for STL strings.
Ever tried type casting??
I think that's itCode:char my_string [1]; cout << "my_string: " << my_string; cout << "int(my_string): " << int(my_string);
Good Luck
what does signature stand for?
initialize it dude....Code:char my_string [1];
char mychar = 'a';
int (mychar) works... strange... but most
books say (int)mychar for typecasting...
Thanks guys... so what you suggested will handle non digit characters such as the negative sign and decimal point, right?
i think atoi() is better than typecasting the thing.
so use atoi()
>i think atoi() is better than typecasting the thing.
>so use atoi()
I agree with moonwalker.
Ok Whatever you sayOriginally posted by moonwalker
initialize it dude....Code:char my_string [1];
char mychar = 'a';
int (mychar) works... strange... but most
books say (int)mychar for typecasting...
what does signature stand for?
>atoi() for null terminated strings.
atoi is notorious for causing problems and most experienced programmers place it with gets to be removed from the language at the first chance. The problem is in how it handles errors, it's far better to use strtol and cast the result to int. Another option is to use sscanf or stringstreams to read an integer value from a C-string or C++ string to an int variable.
-Prelude
My best code is written with the delete key.
Hey, can you make type casting in C ??
what does signature stand for?
int one;
double two = 2.0;
one = (int)two;
c style type casting. Look up dynamic_cast<>, static_cast<>, etc. for c++ style casting.
oh sure, it will let you, in fact C will let you write just about anything you want ("who cares what it means? i just compile it", said the compiler)Originally posted by Ruski
Hey, can you make type casting in C ??
but this:
means something totally different than what you're looking for. (and no, the program doesn't print out 46 when run)Code:#include <stdio.h> int main (void) { char mystring[] = "46"; int i; i = (int) mystring; printf ("%i\n", i); return 0; }
hello, internet!
Thanks for the reply
what does signature stand for?