Jul 10, 2013 at 9:19pm UTC
say I have a string myString("425"), how do I get the value 425 then store it as an int variable?
Thanks in advance.
Last edited on Jul 10, 2013 at 9:20pm UTC
Jul 10, 2013 at 9:22pm UTC
You can use standard function std::stoi
Jul 10, 2013 at 9:23pm UTC
the only thing i can think of is atoi but... wait string streams!!!
u can use string streams! sorry im just really tired right now and didnt think i would be able to come up with something better than atoi
Jul 10, 2013 at 9:39pm UTC
thanks. atoi works but not with characters...
how do we convert characters to its values?
Jul 10, 2013 at 9:44pm UTC
do you mean its ascii value?
Jul 10, 2013 at 9:45pm UTC
You can use sscanf and scan for an int in the char array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <cstdio>
#include <cstdlib>
int StringToInteger(char * s) {
int result;
char dummy;
if (s == NULL) {
printf("NULL string passed to StringToInteger" );
_Exit(1);
}
if (sscanf(s, " %d %c" , &result, &dummy) != 1) {
printf("StringToInteger called on illegal number %s" , s);
_Exit(2);
}
return (result);
}
int main(void ) {
printf("%s+1 = %d" ,"100" ,StringToInteger("100" )+1);
getchar();
}
Oh. You want char to int? Here:
1 2 3 4 5 6 7 8 9 10 11
#include <cstdio>
int CharToInt(char ch) {
return (ch>'9' ||ch<'0' )?-1:ch-48;
}
int main(void ) {
if (1 == CharToInt('1' )) {
printf("Yay, it works!" );
}
}
Last edited on Jul 10, 2013 at 9:58pm UTC
Jul 10, 2013 at 9:46pm UTC
ok im confused. if you want want us offered why wont atoi work? i can make it work for me. and why not string streams?
Jul 10, 2013 at 9:48pm UTC
I pointed out you function
std::stoi . What is the problem?
1 2 3
std::string myString("425" );
std::cout << std::stoi( myString ) << std::endl;
Last edited on Jul 10, 2013 at 9:49pm UTC
Jul 10, 2013 at 9:57pm UTC
stoi also works, now I want to get the value of the individual character myString[0], myString[1], myString[2].
Thank you for all of your responses.
Last edited on Jul 10, 2013 at 9:57pm UTC
Jul 10, 2013 at 9:59pm UTC
jrfrago wrote:stoi also works, now I want to get the value of the individual character myString[0], myString[1], myString[2].
See the 2nd code piece of my post.
DTSCode wrote:ATOI!!!!!!!!
Lol.
Last edited on Jul 10, 2013 at 10:02pm UTC
Jul 10, 2013 at 10:02pm UTC
guys... dont reinvent the wheel! atoi does that!!!!
Jul 10, 2013 at 10:02pm UTC
stoi and atoi work but not with characters...
thanks usandfriends for the code.
Last edited on Jul 10, 2013 at 10:04pm UTC
Jul 10, 2013 at 10:04pm UTC
yes they do. what do you think they do?
atoi takes a character and turns it into an int
Jul 10, 2013 at 10:06pm UTC
jrfrago wrote:stoi and atoi work but not with characters...
I think you need to review stoi and atoi. Here are their links:
http://www.cplusplus.com/reference/string/stoi/
http://www.cplusplus.com/reference/cstdlib/atoi/
DTSCode wrote:guys... dont reinvent the wheel! atoi does that!!!!
jrfrago wrote:thanks usandfriends for the code.
Thanks, but I agree with DTSCode. Functions given in primary libraries are usually:
1. Very fast.
2. Light.
3. Easy to use.
Therefore, I advise against using my code and to stick with
atoi
. Please use the links I posted to learn about them and how to use them. (There are also prime examples at the bottom.)
Last edited on Jul 10, 2013 at 10:10pm UTC
Jul 10, 2013 at 10:13pm UTC
atoi(mySting[0]) gives an error Error 2 error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
but it works with strings...
Jul 10, 2013 at 10:15pm UTC
@DTSCode @Usandfriends
I think we were thinking of the wrong functions. atoi and stoi do the same things.
@jrfrago
Okay, I understand your point. Continue using my function.
Good luck!
Ps: make sure you mark this thread as Solved.
Last edited on Jul 10, 2013 at 10:16pm UTC
Jul 10, 2013 at 10:16pm UTC
@DTSCode
Post your code up here.
Jul 10, 2013 at 10:18pm UTC
hold on i need to complete a wow achievement real quick