create name array help beginner

This is a discussion on create name array help beginner within the C Programming forums, part of the General Programming Boards category; Hi, I'm tryin got import 5 names into an array, then display the array but as you'll see below it's ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    1

    create name array help beginner

    Hi,

    I'm tryin got import 5 names into an array, then display the array but as you'll see below it's displaying the array in a strange way.. i believe i am writing the wrong print f code.

    Also, i am going on form this to ask the user to enter a name and then compare the entered name to each sring in the asrray using a unction. Any ideas?

    code:

    Code:
    #include <stdio.h>
    
    main()
    {  
      char names[5][5]; 
     
     char name1[5] = {'D','I','C','K','Y'};
     char name2[5] = {'J','O','H','N','Y'};
     char name3[5] = {'F','R','E','D','Y'}; 
     char name4[5] = {'P','A','U','L','Y'};
     char name5[5] = {'F','R','A','N','Y'};
    
     int x=0;
     int y=0;
    
           for( x=0; x < 5 ; x++) {
           names[0][x] = name1[x];
       
    	   }  printf("\n");
           for( x=0; x < 5 ; x++) {
           names[1][x] = name2[x];
        
    	   }
           printf("\n");
           for( x=0; x < 5 ; x++) {
           names[2][x] = name3[x];
       
    	   }
           printf("\n");
           for( x=0; x < 5 ; x++) {
           names[3][x] = name4[x];
        
    	   }
    	   printf("\n");
           for( x=0; x < 5 ; x++) {
           names[4][x] = name5[x];
       
    	   }	  
    	   printf("%s\n", names[0]); 
    	   printf("%s\n", names[1]); 
    	   printf("%s\n", names[2]); 
    	   printf("%s\n", names[3]); 
    	   printf("%s\n", names[4]); 
    }
    Print out:

    DICKYJOHNYFREDYPAULYFRANY(smilyface)
    JOHNYFREDYPAULYFRANY(smilyface)
    FREDYPAULYFRANY(smilyface)
    PAULYFRANY(smilyface)
    FRANY(smilyface)

    Thanks in advance

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What you need to do is get yourself a good C book, or perhaps even a good tutorial, and learn C basics. You are trying to print a string with your printf() statements, but you are not giving strings. You are lacking the '\0' chars at the end that tell printf() when to stop printing.

    Furthermore, the general design, not to mention indentation, could... uh... well... be improved.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by moscoworbust View Post
    Hi,

    I'm tryin got import 5 names into an array, then display the array but as you'll see below it's displaying the array in a strange way.. i believe i am writing the wrong print f code.

    Also, i am going on form this to ask the user to enter a name and then compare the entered name to each sring in the asrray using a unction. Any ideas?

    code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {  
      int i; 
      int x=0;
      int y=0;
    
      char names[6][5]; 
     
      char name1[5] = {'D','I','C','K','Y'};
      char names6[6] = {"D6CKY"};
     
      char names7[] = {"D7CKY\0"};
      char names8[] = {"D8CKY"};
    
      char name2[5] = {'J','O','H','N','Y'};
      char name3[5] = {'F','R','E','D','Y'}; 
      char name4[5] = {'P','A','U','L','Y'};
      char name5[5] = {'F','R','A','N','Y'};
    
      for( x=0; x < 5 ; x++) {
         names[0][x] = name1[x];
      }  
      printf("\n");
      for( x=0; x < 5 ; x++) {
         names[1][x] = name2[x];
      }
      printf("\n");
      for( x=0; x < 5 ; x++) {
         names[2][x] = name3[x];
      }
      printf("\n");
      for( x=0; x < 5 ; x++) {
         names[3][x] = name4[x];
      }
      printf("\n");
      for( x=0; x < 5 ; x++) {
         names[4][x] = name5[x];
      }	  
      printf("%s\n", names[0]); 
      printf("%s\n", names[1]); 
      printf("%s\n", names[2]); 
      printf("%s\n", names[3]); 
      printf("%s\n", names[4]); 
    
    
      printf("%s\n", names6);
    
      printf("%s\n", names7);
      x = 0;
      while(names8[x] != '\0')
         printf("%c", names8[x++]);
     
      x = strlen(names6); 
      for(i = 0; i < x; i++)
        printf("%c", names6[i]);
    
      return 0;
    }
    Print out:

    DICKYJOHNYFREDYPAULYFRANY(smilyface)
    JOHNYFREDYPAULYFRANY(smilyface)
    FREDYPAULYFRANY(smilyface)
    PAULYFRANY(smilyface)
    FRANY(smilyface)

    Thanks in advance
    Try some of these variations, and see what you think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. create array with "new"
    By terracota in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2004, 11:14 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. for loop to create an array of objects
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 05-03-2002, 07:26 PM

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