Storing strings in 2d char arrays problem

This is a discussion on Storing strings in 2d char arrays problem within the C++ Programming forums, part of the General Programming Boards category; I am trying to store strings in a 2d array to seperate the text that is between certain charators. It ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    29

    Storing strings in 2d char arrays problem

    I am trying to store strings in a 2d array to seperate the text that is between certain charators. It outputs the strings, but with other charactors what i don't wan't also. Can anyone tell me how to correct this? Thankyou for your time.


    Code:
    #include <iostream.h>
    
    int main()
    {
    char string[5][8];
    char words[30] = "TEXT+TEST+TEXT+TEXT";
    int x,y;
    
    
    for (y = 0; y < 4; y++)
        {
        for (x = 0; x < 4; x++)
            {
            if (words [x] == '+')
                continue;
                
            string[y][x] = words[x];
            }
        }
    
    for (x = 0; x < 4; x++)
        {
        cout << string[x] <<endl;
        }
    
    cin.get();
    return 0;
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    1. You never terminate the strings with the null character. For each string in your array, you should add '\0' to the end of the string, or initialize them all to all 0 before your start.

    2. I'm not sure you program works the way you want after you do this (TEST never gets output), but you can worry about that next.

    3. I'd recommend using the standard C++ library headers. That means <iostream> instead of <iostream.h>. If you do you will have to deal with the std namespace, but simply putting using namepsace std; once under your #includes should be fine for now while you have small programs.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    29
    I wan't to store words in an array sort of like the array below. Everytime i try it it gives me weird results in the output(weird charactors in the output). The code below works fine for me, but the words are already in the array, I want to get words from a string and store them in the array though.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    char array[7][10] = {"Sun","Mon","Tues","Wed","Thurs","Fri","Sat"};
    int x;
    
    for (x=0;x<7;x++)
        {
        cout <<array[x];        
        cout <<endl;
        }
    cin.get();
    return 0;
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Like so
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char string[5][8] = { 0 };
        char words[30] = "TEXT+TEST+TEXT+TEXT";
        int x,y = 0, z = 0;
        for ( x = 0 ; x < strlen(words) ; x++ ) {
            if ( words[x] != '+' ) {
                string[y][z++] = words[x];
            } else {
                string[y++][z] = '\0';
                z = 0;
            }
        }
        string[y][z] = '\0';    // in case words doesn't end in a +
    
        for (x = 0; x < 4; x++)
        {
        cout << string[x] <<endl;
        }
    
        cin.get();
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the process you are describing is commonly called parsing. If you have a specific char you wish to parse on, say the + sign in your example, or the space char, or any other char or combination of char for that matter, then you can use the strtok() function for C++ strings or the find() function for STL strings to determine where each word in the string starts and stops and then add them to the array. Alternatively, you can write your own parsing function by reading the string char by char looking for the termination/start char and storing each char in an array, null terminating the array, and then adding the C string the array of C strings.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    29
    Thankyou for all the help. I get the output I want now!

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    char array[8][8] = { 0 };
    char string[30] = "ONE-TWO+THREE*FOUR/FIVE";
    
    int x, y =0, z = 0;
    
    for (x = 0; x < strlen(string); x++)
        {
        if (string[x] == '-' || string[x] =='+' || string[x] == '/' || string[x] =='*')
            {
            array[y][z] = '\0';
            y++;
            z = 0;  
            }
        else
            {
            array[y][z] = string[x];
            z++;
            }
        }
    array[y][z] = '\0';
    
    for (x = 0; x < 5; x++)
        {
        cout << array[x]<<endl;
        }
    cin.get();
    return 0;
    }
    Last edited by rainmanddw; 10-22-2003 at 06:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ (char *) problem?
    By Teegtahn in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2008, 12:23 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. malloc with arrays of strings
    By Lib in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 10:46 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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