C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2010, 07:48 PM   #1
Registered User
 
Join Date: Jan 2010
Posts: 54
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]);

}
artistunknown is offline   Reply With Quote
Old 02-09-2010, 08:15 PM   #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.
Cameron2 is offline   Reply With Quote
Old 02-09-2010, 08:24 PM   #3
Registered User
 
Join Date: Jan 2010
Posts: 54
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
artistunknown is offline   Reply With Quote
Old 02-09-2010, 08:46 PM   #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.
Cameron2 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking array for string Ayreon C Programming 87 03-09-2009 03:25 PM
singly linked circular list DarkDot C++ Programming 0 04-24-2007 08:55 PM
Linked List Help CJ7Mudrover C Programming 9 03-10-2004 10:33 PM
Contest Results - May 27, 2002 ygfperson A Brief History of Cprogramming.com 18 06-18-2002 01:27 PM


All times are GMT -6. The time now is 12:18 AM.


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