strings

This is a discussion on strings within the C++ Programming forums, part of the General Programming Boards category; First Question. how would i load a file with numbers such as 20 35 10 50 and break them down ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    77

    Question strings

    First Question.

    how would i load a file with numbers such as

    20
    35
    10
    50

    and break them down into 2 arrays

    i.e.

    array[4] = { 2, 3, 1, 5}
    array2[4] = { 0, 5, 0 , 0}

    Second Question.

    how would i change an int array into a char array?

    thanks for any help
    Hooked On Phonics Didn't Work For Me!

  2. #2
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    >>>>how would i load a file with numbers such as

    20
    35
    10
    50

    and break them down into 2 arrays

    i.e.

    array[4] = { 2, 3, 1, 5}
    array2[4] = { 0, 5, 0 , 0}

    This looks cool but have you thought like this maybe.
    array[2] = {20, 35} //your setting up an array of 2 elements with numbers 20 and 35 being initialized into those elements.
    array2[2] = {10, 50} //same thing


    >>>>>>how would i change an int array into a char array?

    char array[4] = {'2', '3', '1', '5'} //must use ' ' for char and " " for strings.

    Is that what your asking for?
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    for the first question though i want to break it down into 1 number each so that i can apply some simple encryption to them
    Hooked On Phonics Didn't Work For Me!

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Read the first char, put it in the first array (element 1).
    Read the next char, put it in the second array (element 1).
    Read the next char, which is the line break, discard it.
    Read the next char, put it in the first array (element 2).
    Read the next char, put it in the second array (element 2).
    Read the next char, which is the line break, discard it.
    ...etc...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Why not...

    Code:
    ifstream infile("/home/user/file.txt", ios::in);
    
    char array1[4];
    char array2[4];
    
    for (int i = 0; i < 4; ++i) {
    
       infile >> array1[i] >> array2[i];
    
    }
    This is the C++ board, right?

    So use C++ extraction operator to pull a char at a time, which already ignores line breaks and such.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    i think i got it now, thanks for the help
    Hooked On Phonics Didn't Work For Me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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