Thread: integer array to char array?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    integer array to char array?

    I'll explain basically what I'm doing.. Since I'm just looking for a step I don't think any code needs to be posted.

    I took a user input and turned it into a integer array (I needed to run some arithmetic operations on the single input). So now I have an integer array w/ stored #'s in each slot. I need to cout corresponding character arrays depending on the value of or each slot of the integer array.

    so as an example: user input's 123. then I turn into a integer array so that I can add 1+2+3=6 and store that value. Each natural number corresponds to a word that needs to be put together in a single cout statement. I just need a way to reference each value in the int array to a single char array each.

    thanx for any help

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just subtract '0' from each character's value, and loop through the character array. No need to make it an int array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    I should explain what I have:

    int array[5] // each of the spaces has an integer value in it, say "1,2,1,2"

    char one[5]="blah"
    char two[5]="wassa"

    now I want to cout "blah wassa blah wassa" b/c user the inputted 1,2,1,2

    I figured several if statements but I need to reference them properly.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    then I turn into a integer array
    So, you're able to turn into an integer array? Wow, cool! And it's not even Halloween yet!

    I'd take it one step at a time. If you need a digit turned into a char, just add 48 (or '0'), to it. Using something like strtol(), or atoi(), is a lot easier for multi-digit numbers.

    Your description of the word requirements seems to indicate you need a 2D char array, organized like:

    Code:
    words[0][] == "zero"
    words[1][] == "one"
    words[2][] == "two"
    //etc.
    Hope that helps.

    As much as I enjoy your "blah wassa, blah wassa" cout (clearly the funniest I've heard in awhile), you need to use an index into a 2D char array, to do what you want.

    If I understand it correctly.
    Last edited by Adak; 10-19-2009 at 09:09 PM.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    sounds good, I'll give the 2D array a shot then.. thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM