How do I input and print out a list of names

This is a discussion on How do I input and print out a list of names within the C Programming forums, part of the General Programming Boards category; I have this code that it starts with a simple menu I can enter some names up to five and ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    150

    How do I input and print out a list of names

    I have this code that it starts with a simple menu I can enter some names up to five and then have a choice to print out the names I'm using a multi dimensional array but I can't figure out how to display the names I think there store in there just not sure here's the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    	int i = 0;
    	char *iName[1][4] =  {'\0'};
           int iChoice = 0;
    
    
    	while(iChoice != 3)
    	{
    		system("cls");
    		printf("\n1\tEnter a name");
    		printf("\n2\tPrint Report");
    		printf("\n3\tQuit");
    		printf("\n\n\tEnter a Choice: ");
    		scanf("%d", &iChoice);
    
    		if(iChoice == 1)
    		{
    			system("cls");
    			printf("\n\tEnter a name: ");
    			scanf("%s %s", iName[0],iName[1]);
    		}
    		else if(iChoice ==2)
    		{
    			break;
    
    		}
    		else if(iChoice == 3)
    			break;
    	}
    	system("cls");
    	for (i = 0; i <= 4; i++)
    		printf("%s %s",iName[0],iName[1]);
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    char *iName[1][4] is a mulidimensional array of pointers to chars. Why not just have an array of pointers to chars like this:

    Code:
    char *iName[4] //This will store 4 pointers to chars (4 strings)
    Then you can do this:

    Code:
    if(iChoice == 1)
    		{
    			system("cls");
    			printf("\n\tEnter a name: ");
    			scanf("%s", iName[0]) //Store the first string;
    		}
    I'm sure you will be able to figure out the rest.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Well I might be able to figure that out but I'm suppose to learn how to use a multdimensional array the program is suppose to teach me how to use them

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    4
    Fair enough. I still don't see why you need a multidimensional array of pointers to chars, how about you just do this:

    Code:
    char iName[Number of Strings you want - 1][Length of string + 1]
    Thus the definition:

    Code:
    char iName[1][4];
    Creates 2 arrays of 5 chars.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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