Numerical value of a string

Pages: 12
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
You can use standard function std::stoi
closed account (Dy7SLyTq)
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
thanks. atoi works but not with characters...
how do we convert characters to its values?
closed account (Dy7SLyTq)
do you mean its ascii value?
closed account (18hRX9L8)
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
closed account (Dy7SLyTq)
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?
@jrfrago


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
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
closed account (Dy7SLyTq)
ATOI!!!!!!!!
closed account (18hRX9L8)
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
closed account (Dy7SLyTq)
guys... dont reinvent the wheel! atoi does that!!!!
stoi and atoi work but not with characters...

thanks usandfriends for the code.
Last edited on
closed account (Dy7SLyTq)
yes they do. what do you think they do?

atoi takes a character and turns it into an int
closed account (18hRX9L8)
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
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...
closed account (18hRX9L8)
@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
closed account (Dy7SLyTq)
it works for me
closed account (18hRX9L8)
@DTSCode

Post your code up here.
closed account (Dy7SLyTq)
hold on i need to complete a wow achievement real quick
Pages: 12