Thread: Trouble with strings and arrays

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

    Trouble with strings and arrays

    Oh hey yall,

    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);
    }

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You have defined an array of 5 arrays of 2 chars aka letters. Shouldn't it be 2 arrays of arrays of letters?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    Quote Originally Posted by Brafil View Post
    You have defined an array of 5 arrays of 2 chars aka letters. Shouldn't it be 2 arrays of arrays of letters?
    .... ohhhhhhhhhhh yes yes

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    Quote Originally Posted by neptunusmaris View Post
    .... ohhhhhhhhhhh yes yes
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 04:24 AM
  2. strings or arrays of characters?
    By Callith in forum C++ Programming
    Replies: 13
    Last Post: 12-26-2004, 11:28 AM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM
  5. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM

Tags for this Thread