separating line of arrays into array of arrays

This is a discussion on separating line of arrays into array of arrays within the C++ Programming forums, part of the General Programming Boards category; If anyone can help -much appreciate I want to take a line of input from the user using an array ...

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

    Question separating line of arrays into array of arrays

    If anyone can help -much appreciate

    I want to take a line of input from the user using an array of cmd[100]; then take that input and separating them out with spaces as a delimiter into an array of arrays cmdarg[][].

    I think the code below does separate them out, but for some reason i can't cout anything other than cmdarg[0]. Nothing else comes out???

    this will be use as a command line with arguments in a program, but i am having a tough time separating the commands and arguments into its own vector of arrays. Is better to do it with strings??? I am not sure of what to use?

    thanks in advance





    //partial codes
    char *c[1000];
    char cmd[100];
    char cmdarg[1000][1000];
    int status,i=0,j=0,k=0;


    cin.ignore();

    cout<<"Commands:";
    cin.getline(cmd,100);

    for(i=0;i<100;i++){
    if(cmd[i]==' '){//space
    j++; //increase row
    k==-1;//reset column
    }
    if(cmd[i]!=' '){//put in next column
    cmdarg[j][k]=cmd[i];//insert in array of arrays
    }
    k++;//increase column
    }
    Last edited by robocop; 10-19-2001 at 06:27 PM.

  2. #2
    Unregistered
    Guest
    is this a typo ?

    k==-1;//reset column

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    k=-1 not a typo

    k=-1 is not a column, because once the space is found, it has to go thru the loop which will increase (k++) to zero again.

    thanks

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Code:
    int main ( ) {
        char cmd[100] = "this is a test";
        char cmd_words[20][100];
        int  i,nwords=0,nword_chars=0;
        for ( i = 0 ; i < strlen(cmd) ; i++ ) {
            if ( cmd[i] != ' ' ) {
                // copy the char, and keep the word a proper string (the \0)
                cmd_words[nwords][nword_chars++] = cmd[i];
                cmd_words[nwords][nword_chars] = '\0';
            } else {
                nword_chars = 0;  // start of...
                nwords++;  // the next word
            }
        }
        // trap last word not ending in a space
        if ( nword_chars != 0 ) nwords++;
    
        for ( i = 0 ; i < nwords ; i++ ) {
            printf( "Word %d is %s\n", i, cmd_words[i] );
        }
        return 0;
    }
    > char cmdarg[1000][1000];
    Bear in mind that this is almost 1MB in size - you don't want too many of these lying around.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. delete array of arrays
    By tmod in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2007, 03:37 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 08:53 PM

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