C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-08-2009, 05:05 AM   #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
moscoworbust is offline   Reply With Quote
Old 06-08-2009, 05:09 AM   #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.
__________________
MacGyver is offline   Reply With Quote
Old 06-08-2009, 06:04 AM   #3
Registered User
 
Join Date: Sep 2006
Posts: 3,142
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.
Adak is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:59 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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