![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 1
| create name array help beginner 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]);
}
DICKYJOHNYFREDYPAULYFRANY(smilyface) JOHNYFREDYPAULYFRANY(smilyface) FREDYPAULYFRANY(smilyface) PAULYFRANY(smilyface) FRANY(smilyface) Thanks in advance |
| moscoworbust is offline | |
| | #2 |
| Deathray Engineer 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 | |
| | #3 | |
| Registered User Join Date: Sep 2006
Posts: 3,142
| Quote:
| |
| Adak is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |