2D Char Array

This is a discussion on 2D Char Array within the C Programming forums, part of the General Programming Boards category; Here is my issue. Inside the function get_data the 2D array names prints and operates fine. However, once in main. ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    2D Char Array

    Here is my issue. Inside the function get_data the 2D array names prints and operates fine. However, once in main. It prints this:
    Am

    An

    Be

    Dan

    Instead of:

    Ami
    Ann
    Ben
    Dan

    Heres the code:

    Code:
    int main (void)
    {
    //Local Declarations
    int size;
    int students_friends[MAX_STUDENTS][MAX_STUDENTS] = { {0 } };
    int number_friends[MAX_STUDENTS];
    char names[MAX_STUDENTS][NUM_NAMES] = { {0} };
    
    
    //Statements
    size = get_data(students_friends, names, size);
    frequency_friends(students_friends, number_friends, size);
    //list_friends(students_friends, names,number_friends, size);
    
    for (int row = 0; row < size; row++)
    for (int col = 0; col < size; col++)
    printf("%c", names[row][col]);
    
    
    }
    
    int get_data (int students_friends[][MAX_STUDENTS],char names[][NUM_NAMES],
    int size)
    {
    //Local Declarations
    int data_in;
    char name_data;
    int loader;
    FILE* fp_data;
    char whitespace;
    whitespace = NULL;
    
    //Statements
    loader = 0;
    
    //Checks that file opens
    if ((fp_data = fopen ("clubs.txt", "r")) == NULL)
    {
    printf("Error opening file\a\a\n") ,
    exit (100);
    }
    //Reads first integer in text file and assigns it to size
    if (fscanf(fp_data, "%d", &data_in) != EOF && loader == 0)
    {
    size = data_in;
    printf("%d", size);
    loader++;
    }
    //Reads the names in text file into 2D array
    for (int row = 0; row < size; row++)
    for (int col = 0; col < size; col++)
    if (loader > 0 && loader <= (size * size) &&
    fscanf(fp_data, "%c", &name_data) != EOF
    && name_data != whitespace)
    {
    names[row][col] = name_data;
    printf("%c", names[row][col]);
    loader++;
    }
    printf("\n");
    //Reads the remaining numbers in text file into 2D array
    for (int row = 0; row < size; row++)
    for (int col = 0; col < size; col++)
    if (loader > (size * size) && loader <= (size * size) + (size * size) &&
    fscanf(fp_data, "%d", &data_in) != EOF)
    
    {
    students_friends[row][col] = data_in;
    printf("%d ", students_friends[row][col]);
    //After four numbers, prints a new line
    if (!(loader % size))
    printf("\n");
    loader++;
    }
    return size;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Read this, then re-post your code so that it is indented. Then people might read it.
    SourceForge.net: Indentation - cpwiki
    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.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    8
    Code:
    int main (void)
    {
    //Local Declarations
    int size;
    int students_friends[MAX_STUDENTS][MAX_STUDENTS] = { {0 } };
    int number_friends[MAX_STUDENTS];
    char names[MAX_STUDENTS][NUM_NAMES] = { {0} };
    
    
    //Statements
    size = get_data(students_friends, names, size);
    frequency_friends(students_friends, number_friends, size);
    //list_friends(students_friends, names,number_friends, size);
    
    for (int row = 0; row < size; row++)
       for (int col = 0; col < size; col++)
            printf("%c", names[row][col]);
    
    
    }
    
    int get_data (int students_friends[][MAX_STUDENTS],char names[][NUM_NAMES],
    int size)
    {
    //Local Declarations
    int data_in;
    char name_data;
    int loader;
    FILE* fp_data;
    char whitespace;
    whitespace = NULL;
    
    //Statements
    loader = 0;
    
    //Checks that file opens
    if ((fp_data = fopen ("clubs.txt", "r")) == NULL)
       {
          printf("Error opening file\a\a\n") ,
         exit (100);
       }
    //Reads first integer in text file and assigns it to size
    if (fscanf(fp_data, "%d", &data_in) != EOF && loader == 0)
        {
         size = data_in;
         printf("%d", size);
         loader++;
        }
    //Reads the names in text file into 2D array
     for (int row = 0; row < size; row++)
        for (int col = 0; col < size; col++)
            if (loader > 0 && loader <= (size * size) &&
               fscanf(fp_data, "%c", &name_data) != EOF
               && name_data != whitespace)
             {
                 names[row][col] = name_data;
                 printf("%c", names[row][col]);
                 loader++;
             }
    printf("\n");
    //Reads the remaining numbers in text file into 2D array
    for (int row = 0; row < size; row++)
          for (int col = 0; col < size; col++)
               if (loader > (size * size) && loader <= (size * size) + (size * size) &&
                  fscanf(fp_data, "%d", &data_in) != EOF)
                 {
                   students_friends[row][col] = data_in;
                  printf("%d ", students_friends[row][col]);
                  //After four numbers, prints a new line
                   if (!(loader % size))
                   printf("\n");
                  loader++;
                 }
    return size;
    }
    The red part indicates where during the for loop the 2D array "names" prints correctly. However, when I try to print "names" anywhere else. It comes out wrong. Any help guys? I believe it is just a logic error that I can't find, but perhaps its something else?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Hmpf, so much for indentation.

    Indentation means it should look something like this.
    Code:
    int main (void)
    {
        //Local Declarations
        int size;
        int students_friends[MAX_STUDENTS][MAX_STUDENTS] = { {0 } };
        int number_friends[MAX_STUDENTS];
        char names[MAX_STUDENTS][NUM_NAMES] = { {0} };
    
        //Statements
        size = get_data(students_friends, names, size);
        frequency_friends(students_friends, number_friends, size);
        //list_friends(students_friends, names,number_friends, size);
    
        for (int row = 0; row < size; row++)
            for (int col = 0; col < size; col++)
                printf("%c", names[row][col]);
    }
    
    int get_data (int students_friends[][MAX_STUDENTS],char names[][NUM_NAMES],
                  int size)
    {
        //Local Declarations
        int data_in;
        char name_data;
        int loader;
        FILE* fp_data;
        char whitespace;
        whitespace = NULL;
    
        //Statements
        loader = 0;
    
        //Checks that file opens
        if ((fp_data = fopen ("clubs.txt", "r")) == NULL)
        {
            printf("Error opening file\a\a\n") ,
            exit (100);
        }
    
        //Reads first integer in text file and assigns it to size
        if (fscanf(fp_data, "%d", &data_in) != EOF && loader == 0)
        {
            size = data_in;
            printf("%d", size);
            loader++;
        }
    
        //Reads the names in text file into 2D array
        for (int row = 0; row < size; row++)
            for (int col = 0; col < size; col++)
                if (loader > 0 && loader <= (size * size) &&
                        fscanf(fp_data, "%c", &name_data) != EOF
                        && name_data != whitespace)
                {
                    names[row][col] = name_data;
                    printf("%c", names[row][col]);
                    loader++;
                }
        printf("\n");
    
        //Reads the remaining numbers in text file into 2D array
        for (int row = 0; row < size; row++)
            for (int col = 0; col < size; col++)
                if (loader > (size * size) && loader <= (size * size) + (size * size) &&
                        fscanf(fp_data, "%d", &data_in) != EOF)
                {
                    students_friends[row][col] = data_in;
                    printf("%d ", students_friends[row][col]);
                    //After four numbers, prints a new line
                    if (!(loader % size))
                    printf("\n");
                    loader++;
                }
        return size;
    }
    The first thing you might try is using braces in a few more places, especially your nested for loops.
    They're easy to add, and you're not going to trip yourself (or your code readers) up over the finer points of what constitutes a statement.

    Oh, and whitespace isn't NULL.
    Generally, use the isspace() function in ctype.h
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Why is C posted in the C++ forum?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Salem View Post
    Generally, use the isspace() function in ctype.h
    If this is C++, that should read "cctype" ;-).
    And then Elysia will be happy, since it's no longer valid C but valid C++ ;-). Well, kinda...

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Happy? I am not so quite happy with fscanf, printf, 2D char arrays, etc.
    You can never turn this code into C++, but "C+" might be possible.
    Nevertheless, the C++ board is being infected by C! Something must be done!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Yeah, looks closer to C than C++
    Though the for(int loops probably mean it needs a C++ compiler if the OP isn't using a proper C99 compiler (eg. anything by microsoft)

    Moved to C for now.
    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.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,231
    Hmpf, so much for indentation.
    Heh. Fool, you.

    Soma

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    8
    Code:
    //Reads the names in text file into 2D array
       for (int row = 0; row < size; row++)
       {
               for (int col = 0; col < size; col++)
               {
                  if (loader > 0 && loader <= (size * size) &&
                     fscanf(fp_data, "%c", &name_data) != EOF
                     && !isspace(name_data))
                    {
                       names[row][col] = name_data;
                       loader++;
                    }
               }
       }
    I added the isspace, but now when printed the data looks like this:
    AmiiAnnnBennDan

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Are you able to debug?

    Code:
        for (int row = 0; row < size; row++)
            for (int col = 0; col < size; col++)
                printf("%c", names[row][col]);
    Should row and col both use size as the upper bounds for printing. Doesn't seem right...

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Elysia View Post
    Happy? I am not so quite happy with fscanf, printf, 2D char arrays, etc.
    You can never turn this code into C++, but "C+" might be possible.
    Nevertheless, the C++ board is being infected by C! Something must be done!
    Off topic, but that's the OP's fault, he should have posted in the right forum...
    I think that's because "C++" is the TOP forum. People don't want to read so pick the first option more easily. And yes, C quite commonly would fit in a C++ forum. You can't even blame the guy: it's still legal C++ code is it not?
    My theory is that it would matter a lot if the orders are reversed. First the C forum, then C++. People will see the C forum first and post there - unless it's C++, in which most will know "that's not the right language", and move on to the next one - C++.

    So my theory is basically it's easier to say "A C++ post doesn't belong in a C forum" than "A C post doesn't belong in a C++ forum", hence the order would matter for lazy people.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uincode char array to array<unsigned char,1>^
    By ripspinner in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2009, 04:41 PM
  2. Replies: 3
    Last Post: 11-17-2008, 11:36 AM
  3. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 06:19 PM
  4. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 05:18 PM
  5. Converting const char to char array
    By HomerJ in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 03:21 PM

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