2D Array's, assigning chars.

This is a discussion on 2D Array's, assigning chars. within the C Programming forums, part of the General Programming Boards category; I've become quite frustrated trying to assign input into 2D Arrays. What's the best way to do it? I have ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    2D Array's, assigning chars.

    I've become quite frustrated trying to assign input into 2D Arrays. What's the best way to do it?

    I have a 2D array of char type that is initially empty. I want to print out the array once the user has entered 5 rows of input.

    I've tried using fgets to assign each character to each position:

    Code:
    #define ROWS 5
    #define COLMNS 5
    
    array[5][5];
    
    while(y < COLMNS) {
       fgets(board[i], ROWS, stdin);
       i++;
       y++  ;
    }
    I think when the user enters 'abcd' for example, fgets assigns the whole input to [0,0] (during the first loop) instead of each character to [1, 0], [2,0], ..., [5,0] ???

    I also tried making a temporary 1D array like so:

    Code:
    #define ROWS 5
    #define COLMNS 5
    
    int y = 0;
    int i = 0;
    
    array[5][5];
    line[5];
    
    while(y < COLMNS) {
       for(i = 0; i < 5; i++) {
       line[i] = getchar();
       line[i] = array[i][y]
       }
       i = 0;
       y = y + 1;
    
    }
    The logic behind the above was to assign each row individually in the for loop and when it reached 5 it would switch to the next row.

    I also tried a method similar to above but using strcpy to copy the 1D string into the first row of the 2D array but I wasn't sure how to do it correctly.


    I really would appreciate help, sorry if some of these questions seem stupid, I am a bit of a n00b. Kind regards.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    array[5][5]; - you miss the type

    fgets assigns the whole input to [0,0] - you are wrong if array[0][0] is a char it cannot hold 5 chars

    fgets(board[i], - you do not have board defined

    while(y < COLMNS) - you are reading the whole line - get rid of this column loop

    second example has even less sence in it...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Sorry, I actually didn't copy the code from the program, I just rushed it quickly. This is how it was:

    Code:
    #define ROWS 5
    #define COLMNS 5
    
    char array[5][5];
    
    while(y < COLMNS) {
       fgets(array[i], ROWS, stdin);
       i++;
       y++  ;
    }
    Yeah the second example is quite stupid, but what I want is just the user to enter input, hitting return for a new line(I'm actually trying to avoid using fgets to be honest). Example :

    Enter chars:
    1 . 4 a g
    1 2 3 4 5
    . . ! t x
    a b c d e
    5 4 3 2 1

    Your array is:
    1 . 4 a g
    1 2 3 4 5
    . . ! t x
    a b c d e
    5 4 3 2 1

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Maybe something like:
    Code:
    #define ROWS 5
    #define COLMNS 5
    
    char array[ROWS][COLMNS];
    char line[6];
    
    y = 0;
    while(y < ROWS) {
       fgets(line, sizeof line, stdin);
       memcpy(array[y], line, COLMNS);
       y++  ;
    }
    Or using getchar():
    Code:
    #define ROWS 5
    #define COLMNS 5
    
    char array[ROWS][COLMNS];
    int ch, r, c;
    
    for (r=0; r<ROWS; r++)
    {
       c = 0;
       while ((ch = getchar()) != '\n')
       {
          if (c < COLMNS) array[r][c++] = ch;
       }
    }
    Last edited by swoopy; 04-26-2008 at 02:13 AM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    > I think when the user enters 'abcd' for example,
    Don't forget the \n and \0

    fgets will store the abcd\0 on the first round, but will then immediately return with \n\0 for the second round.

    char buff[BUFSIZ];
    fgets( buff, sizeof buff, stdin );

    Then you validate the input in buff (is it too long say), then copy that data to where you really want it to be.
    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.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Thanks for the help guys, but I'm still having problems. So far I've got this:

    Code:
        
    int i, j;
    char input_line[6];
    char array[5][5]
    
      for(i = 0; i < 5; i++) {
          fgets(input_line, 6, stdin);
            for(j = 0; j <= 5; j++) {
              if(input_line[i] != '\n') {
                array[i][j] = input_line[j];
              }
            }
        }
    But the output I'm getting from:
    1
    2
    3
    4
    5

    Is this:

    1
    (weird characters)
    3
    4
    5

    I'm assuming it's a problem with the \n or \0 but not sure. Is there anyway I can use 'strcpy' to copy the 1D array 'input_line' to the first line of the 2D array? So I could then loop it to get the 5 rows.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Should that not be if(input_line[j] != '\n')?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    In addition to tabstop's comment, and based on Salem's comment, I think you might be better making the array size input_line larger, so that the newline also gets stored.
    Code:
    char input_line[20];
    .
    .
          fgets(input_line, 20, stdin);
    This ensures the newline also gets read, and isn't read by the next call to fgets().

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    There are FAQ entries on using fgets() and stripping the \n from the input.
    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.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Heh... good ol' Salem. Sometimes I think Salem just logs in and does four searches then logs off.

    Search one: Salem inside a thread
    Search two: void main()
    Search three: fgets()
    Search four: fflush(stdin)

    Then on the other hand is Kermi... who I strongly believe only logs in when one dares mention Kermi... That and mass hysteria or perhaps catastrophic forum failure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-10-2007, 05:54 AM
  2. 2D Arrays: Column/Row Order
    By Ashes999 in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2003, 08:03 PM
  3. pointers to 2d arrays
    By Diamonds in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2002, 05:58 AM
  4. Passing 2D arrays by reference
    By samGwilliam in forum C++ Programming
    Replies: 9
    Last Post: 04-27-2002, 12:20 PM
  5. 2D arrays with functions made easy
    By goran in forum C Programming
    Replies: 1
    Last Post: 09-17-2001, 12:08 PM

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