Thread: Array and output.

  1. #1
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59

    Array and output.

    My exercise is to create 3 arrays of 6 field each and store a value in them. After that they need to be placed into a 30 field array with the use of "strcat" and the result needs to be printed to the screen 10 times.

    I replaced the "PAUSE" with getchar() as I was advised to in my previous topic, Could someone look this over for me and comment on it if something should be changed or if it is good the way it is written?

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
        char firstValue[6];
        char secondValue[6];
        char thirthValue[6];
        char mixed[30];
        
        int counter;
        
        strcpy(firstValue, "James");
        strcpy(secondValue, "Hornet");
        strcpy(thirthValue, "Watson");
        
        strcpy(mixed, firstValue);
        strcat(mixed, " ");
        strcat(mixed, secondValue);
        strcat(mixed, " ");
        strcat(mixed, thirthValue);
        
        for(counter = 0; counter < 10; counter ++)
        printf("%s\n", mixed);
        
        getchar();
        return 0;
    }
    Last edited by JOZZY& Wakko; 11-05-2009 at 03:39 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Looks OK. You could use #defines for the 6 and 10.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by JOZZY& Wakko View Post
    My exercise is to create 3 arrays of 6 field each and store a value in them. After that they need to be placed into a 30 field array with the use of "strcat" and the result needs to be printed to the screen 10 times.

    I replaced the "PAUSE" with getchar() as I was advised to in my previous topic, Could someone look this over for me and comment on it if something should be changed or if it is good the way it is written?

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
        char firstValue[6];
        char secondValue[6];
        char thirthValue[6];
        char mixed[30];
        
        int counter;
        
        strcpy(firstValue, "James");
        strcpy(secondValue, "Hornet");
        strcpy(thirthValue, "Watson");
        
        strcpy(mixed, firstValue);
        strcat(mixed, " ");
        strcat(mixed, secondValue);
        strcat(mixed, " ");
        strcat(mixed, thirthValue);
        
        for(counter = 0; counter < 10; counter ++)
        printf("%s\n", mixed);
        
        getchar();
        return 0;
    }
    There are some errors. For example, you're putting words 6 chars long in an array[6], so there's no place for the terminating \0.

    Also, talking about style, it should be like this:
    Code:
    for(counter = 0; counter < 10; counter++)
        printf("%s\n", mixed);
    Last edited by MisterIO; 11-05-2009 at 05:54 PM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Good catch MisterIO - I missed the out of bounds.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by MisterIO View Post
    There are some errors. For example, you're putting words 6 chars long in an array[6], so there's no place for the terminating \0.
    Arrays start at 0. There are 7 elements, enough for 6 characters and the terminating NUL character.

    EDIT: You learn something new every day . My bad, thanks for clearing that up Subsonics.
    Last edited by DeadPlanet; 11-06-2009 at 08:07 AM.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by DeadPlanet View Post
    Arrays start at 0. There are 7 elements, enough for 6 characters and the terminating NUL character.
    Yes, but the arrays are declared to have 6 elements. The array indexes would be 0 - 5.

  7. #7
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Thanks.

    I forgot about the closing of an array. My apologies for the late reply but I was out of town for a few day's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Replies: 4
    Last Post: 04-03-2007, 05:57 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM