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
nstefanovAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
The easiest way would be to

for (int i = 0; i < line.length(); i++)
{
   result = line[i] - '0';
}
0
DanRollinsCommented:
int main ()
{
      string line = "123456..etc";
      string sOneDigit = "";
      int result;

      for (int i = 0; i < line.length(); i++) {
            sOneDigit= line[i];                 // a one-character string
            result = atoi( sOneDigit.c_str() );
            cout << result << endl;
      }
      return 0;
}

This version lets you continue to use atoi()... but it breaks out each digit of the original line into a separate string.  That lets you avoid using the "little secret" that the ASCII value of digits '0' - '9' equate to a corresponding binary value when you subtract exactly 0x30 ('0').  I understand that in a future version of the the ANSI STL spec, the digits are not guaranteed to be in any particular order (just kidding).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

nstefanovAuthor Commented:
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
0
jkrCommented:
>>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.
0
nstefanovAuthor Commented:
I meant explanation.  Thanks again.

Nick
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.