multidimensional array - newbie help

This is a discussion on multidimensional array - newbie help within the C Programming forums, part of the General Programming Boards category; I have an array called person (person[10][4]) and I want to allow users to enter person information. The person array ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    multidimensional array - newbie help

    I have an array called person (person[10][4]) and I want to allow users to enter person information. The person array is char. I am confused about the scan part and Im not sure how to put the user input into the array to look like

    James Bond 13 Smith 11111111
    Bunny 17 disney 22222222

    for example james bond would have his name in [1][1] address [1][2] phone num [1][3] in the array.

    I have gathered that I have to use a for loop
    Code:
    #include<stdio.h>
    
    #define numRows 10 /* number of rows */
    #define numCols 4 /* number of columns */
    
    int main (void)
    {
    	/*declare variables */
    	int i, j;
    	char employee[numRows][numCols];
    
    	/*Allow a user to enter employee's details*/
    	printf("Please enter the employee's firstname, lastname, pay classification (in $) and hours worked.");
    			
    	for (i = 0; i < numRows; ++i)
    		for (j = 0; i < numCols; ++j)
    			scanf("%s", &employee[i][j]);
    	return 0;
    }/*end main() function */
    Have I done this right? If not what do I have to do to fix it?
    Last edited by trixxma; 03-24-2006 at 07:34 PM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    *try using code tags
    *don't use #define if you don't have to
    *you need to put your code in a main routine
    *where is this for loop you speak of
    *this is the c++ board, and your code is in C.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    I have updated my code with more detail and code tags.
    Also I was thinking could I scan each input for example
    Code:
    printf("please enter person's name");
    scanf("%s", &a);
    printf("please enter person's address");
    scanf("%s", &b);
    employee[i][j] = {&a, &b};

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by trixxma
    I have an array called person (person[10][4]) and I want to allow users to enter person information. The person array is char. I am confused about the scan part and Im not sure how to put the user input into the array to look like

    James Bond 13 Smith 11111111
    Bunny 17 disney 22222222

    for example james bond would have his name in [1][1] address [1][2] phone num [1][3] in the array.

    I have gathered that I have to use a for loop
    Code:
    #include<stdio.h>
    
    #define numRows 10 /* number of rows */
    #define numCols 4 /* number of columns */
    
    int main (void)
    {
    	/*declare variables */
    	int i, j;
    	char employee[numRows][numCols];
    
    	/*Allow a user to enter employee's details*/
    	printf("Please enter the employee's firstname, lastname, pay classification (in $) and hours worked.");
    			
    	for (i = 0; i < numRows; ++i)
    		for (j = 0; i < numCols; ++j)
    			scanf("%s", &employee[i][j]);
    	return 0;
    }/*end main() function */
    Have I done this right? If not what do I have to do to fix it?
    Have you learned structures yet? If not, let me tell you that you can do this far more effeciently with structures. Example:

    Code:
    struct Person
    {
    	int age;
    	char firstName[12];
    	char lastName[12];
    	char address[30];
    	char phoneNumber[11];
    };
    
    struct Person people[5]; // array: can hold info for up to 10 persons
    But if that's not an option, fine. Your person multidimensional array is capable of holding information on 10 people, *but* each only have 4 bytes of space, so you would need to up the element amount on the second index from 4 to, say, maybe 50:

    char person[10][50];

    I personally would discourage the use of scanf, since it's really error-prone and tougher to get right. I encourage you to use fgets instead. It's just like gets but this one is *safer*. Anyway, you would fill each dimension of the array like this:

    fgets(person[0], MAX_COUNT, stdin);

    and you would do the same for person[1] all the way to person[9] - which is 10 persons because array indexing starts at 0, not 1. And of course, *you*, would determine the length for each piece of data (name, address, phone etc..) to assign to each dimension in a way as to not go over your dimensions bounds.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Think of what you are storing like a database. In a database you need a record, some feilds and the size of those fields.

    Code:
    #include<stdio.h>
    #include <string.h>
    
    int main (void)
    {
    	int index1, index2;
    	/* 2 records with 2 fields of size 40 */
    	char employee[2][2][40];
    	char temp[40];
    
    	printf("Please Enter first and last name\n");
    	
    	for(index1 = 0; index1 < 2; index1++)
    		for(index2 = 0; index2 < 2; index2++)
    		{
    			scanf("%s", &temp);
    			strcpy(employee[index1][index2], temp);
    		}
    	printf("\n\n Hello Mr %s, %s\n", employee[0][1], employee[0][0]);
    	printf("And hello to you Mr %s, %s\n", employee[1][1], employee[1][0]);
    
    	return 0;
    }
    now iv simplified things a bit, and made the output so you can see what you have enterd..
    This is for two records with 2 fields of size 40

    remeber the size

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    scanf("%s", &temp);
    Why not use gets()?

    Read the FAQ (search for "fgets").

    Code:
    scanf("%s", &temp);
    strcpy(employee[index1][index2], temp);
    Why not just read the data into employee[][] to begin with?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    tama00 - the problem with your code it has a warning (warning: format '%s' expects type 'char *', but argument 2 has type 'char (*) [39u]' and my program can not have any warnings.

    dwks - how would you suggest that I read the data straight into the array? like below?
    Code:
    scanf("%s", employee[index1][index2]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 11:19 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Multidimensional Array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2001, 05:18 PM

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