![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 7
| Trouble with strings and arrays I'm having a problem with a program with these directions: " Create a program that allows a user to enter up to five names of friends. Use a two-dimensional array to store the friends’ names. After each name is entered, the user should have the option to enter another name or print out a report that shows each name entered thus far. " Every time I choose to print out the names it doesn't print them out correctly. And here is my code so far: Code: /********************************************************************
Name: <sourcename>
Author: <author>
Description: <description>
*********************************************************************/
#include <stdio.h>
void menu(void);
int menu_op = 1;
int main(void) {
char names[5][2];
printf("\tName Entry (Enter up to 5 names)\n\n");
int a = 0;
for ( ; a < 5; a++) {
names[a][0] = '\0';
names[a][1] = '\0';
}
int i = 0;
for ( ; i < 5; i++) {
if (menu_op == 1) {
printf("Enter first name and last name separated by a space: ");
scanf("%s%s", &names[i][0], &names[i][1]);
}
if (menu_op == 2) {
int j = 0;
printf("\nNames that have been entered:");
for ( ; j < 5; j++)
printf("\n\t%s %s", &names[j][0], &names[j][1]);
}
if (menu_op == 3)
break;
menu();
}
//printf("\n\t%s %s", &names[0][0], &names[0][1]);
int j = 0;
printf("\nNames that have been entered:");
for ( ; j < 5; j++)
printf("\n\t%s %s", &names[j][0], &names[j][1]);
return 0;
}
void menu(void) {
printf("\nMenu:\n");
printf("\n1 Enter another name");
printf("\n2 Show names entered");
printf("\n3 Quit");
printf("\nEnter menu option: ");
scanf("%d", &menu_op);
}
|
| neptunusmaris is offline | |
| | #2 |
| Making mistakes Join Date: Dec 2008
Posts: 347
| You have defined an array of 5 arrays of 2 chars aka letters. Shouldn't it be 2 arrays of arrays of letters? |
| Brafil is offline | |
| | #3 |
| Registered User Join Date: Jun 2009
Posts: 7
| |
| neptunusmaris is offline | |
| | #4 |
| Registered User Join Date: Jun 2009
Posts: 7
| I'm such an idiot ...I'm sooo used to php and python arrays as ive been programming with those languages for long time and get really confused... Oh and thanks for the tip! Here is the fixed code: Code: /********************************************************************
Name: <sourcename>
Author: <author>
Description: <description>
*********************************************************************/
#include <stdio.h>
void menu(void);
int menu_op = 1;
int main(void) {
char names[5][30];
printf("\tName Entry (Enter up to 5 names)\n\n");
int i = 0;
for ( ; i < 5; i++) {
if (menu_op == 1) {
printf("Enter a name: ");
scanf("%s", names[i]);
}
if (menu_op == 2) {
int j = 0;
printf("\nNames that have been entered:");
for ( ; j < 5; j++)
printf("\n\t%s", names[j]);
}
if (menu_op == 3)
break;
menu();
}
//printf("\n\t%s", &names[0]);
int j = 0;
printf("\nNames that have been entered:");
for ( ; j < 5; j++)
printf("\n\t%s", names[j]);
return 0;
}
void menu(void) {
printf("\nMenu:\n");
printf("\n1 Enter another name");
printf("\n2 Show names entered");
printf("\n3 Quit");
printf("\nEnter menu option: ");
scanf("%d", &menu_op);
}
Last edited by neptunusmaris; 06-19-2009 at 05:59 AM. Reason: Saying thank you |
| neptunusmaris is offline | |
![]() |
| Tags |
| arrays, strings |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help understanding arrays of strings! | smoking81 | C Programming | 18 | 02-23-2008 04:24 AM |
| strings or arrays of characters? | Callith | C++ Programming | 13 | 12-26-2004 11:28 AM |
| Sorting Multiple Arrays with complications of 2D arrays and strings. | compiler151 | C Programming | 2 | 02-23-2004 06:34 AM |
| Storing strings in 2d char arrays problem | rainmanddw | C++ Programming | 5 | 10-22-2003 05:41 PM |
| strings or character arrays | Shadow12345 | C++ Programming | 2 | 07-21-2002 10:55 AM |