Converting a "Char" string to an Integer Array

This is a discussion on Converting a "Char" string to an Integer Array within the C++ Programming forums, part of the General Programming Boards category; I need to convert a string of 10 characters to an integer number and add them all together. For example, ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    Question Converting a "Char" string to an Integer Array

    I need to convert a string of 10 characters to an integer number and add them all together.

    For example, the string I enter is;

    1111111111

    I need to be able to convert that into an array and then add them eg;

    array[1] + array [2] + array [3] etc etc etc.

    Can anyone help me or point me in the right direction?

    Thanks in advance for any help anyone can give me with this.

  2. #2
    Unregistered
    Guest
    #include <stdlib.h>

    int nums[strlen(string)]; // <--- An array of ints

    for(i = 0; i< strlen(string); i++)
    {
    nums[i] = atoi(string[i]);
    }


    Can you figure out how to add them all together?

  3. #3
    Unregistered
    Guest
    Start with the right most number and then multiply the next number by 10 before adding it the the running total. Keep doing this and you will get the correct total.

  4. #4
    Unregistered
    Guest
    I'm sorry, multiply the next number by 10, then the next by 100, and so on...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Converting an integer into a string
    By Signifier in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2007, 11:56 AM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. Converting string to integer...
    By Ham in forum C++ Programming
    Replies: 6
    Last Post: 01-26-2003, 01:43 AM
  5. Converting a string to an integer
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2001, 11:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21