nstefanov

asked on

# Parsing string

I need to parse a string of integers like this:

string line = "123456..etc";

int result;

I need every single digit converted to a number so I am doing something like this:

for (int i = 0; i < line.length(); i++)

{

result = atoi(&line[i]);

}

Instead of breaking the string up I get:

123456

23456

3456, Etc.

What is the best way to do this. Thank you very much ahead of time.

Nick

string line = "123456..etc";

int result;

I need every single digit converted to a number so I am doing something like this:

for (int i = 0; i < line.length(); i++)

{

result = atoi(&line[i]);

}

Instead of breaking the string up I get:

123456

23456

3456, Etc.

What is the best way to do this. Thank you very much ahead of time.

Nick

ASKER CERTIFIED SOLUTION

membership

Create a free account to see this answer

Signing up is free and takes 30 seconds.

**No credit card required.**
?

ASKER

jkr your method also works great i just thought Dan's is a little more complete. Maybe should have split the points. Will have to find out how to do that. Thanks for your help.

Nick

Nick

>>i just thought Dan's is a little more complete

To what extent? You wanted to convert chars into ints, not chars into strings that you then convert to ints.

To what extent? You wanted to convert chars into ints, not chars into strings that you then convert to ints.

ASKER

I meant explanation. Thanks again.

Nick

Nick

for (int i = 0; i < line.length(); i++)

{

result = line[i] - '0';

}