Link to home
Start Free TrialLog in
Avatar of nstefanov
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
Avatar of jkr
jkr
Flag of Germany image

The easiest way would be to

for (int i = 0; i < line.length(); i++)
{
   result = line[i] - '0';
}
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
?
Avatar of nstefanov
nstefanov

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
>>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.
I meant explanation.  Thanks again.

Nick