2 dimensional array asking for number of names and the name

This is a discussion on 2 dimensional array asking for number of names and the name within the C Programming forums, part of the General Programming Boards category; hi im your typical noob to C programming and am asking for some help on a recent program im working ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    37

    2 dimensional array asking for number of names and the name

    hi im your typical noob to C programming and am asking for some help on a recent program im working on for my intro to C class.

    in my program i have to enter payroll information for employees and my professor wants us to use a 2-dimensional array to count for the number of employees and the amount of letters in their name.

    so right now if i have this:

    Code:
    char emp_name[30][10];
    
    /* Welcome and prompt for number of employees */
    printf ("Enter the number of employees (1-10): ");
    scanf ("%i", &emp_name[30][10]);
    printf ("\n");
    how would i use the char emp_name[30][10] to count for the number of employees, and then later use that to prompt for that users name?

    thank you for any help

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    I'm curious... why do you think this will work?
    scanf ("%i", &emp_name[30][10]);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    i have no idea. i know it wont work lets put it at that. just trying to give an idea of what i am looking to do.

    basically i want to ask the user for how many names they want to enter between 1 and 10 and then later ask for the name for the number of names entered with a max of 30 characters.

    im lost.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    the professor gave us the algorithm for the code we are suppose to write and only gave emp_name[30][10] and not like just an int to count for the number of employees. so im figuring he wants us to use the multidimensional array to count for the number of employees.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    I figure he wants you to use the 2D array to store the employee names.
    A name consists of several characters, so to store a name, you would need several characters, a string, as it is called.

    char [x] = string

    So if we then would need to store several names, then we would need an array of strings, no?
    That would be something like

    string [y]

    And since string = char [x], that becomes

    char [y][x]

    You need to create more variables to store other things.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    yep, i see where i am making my mistake the [10] part of emp_name is just to count for the maximum of the 10 names that can be entered, but i would need another variable like int num_emp to count for the number of employees actually being entered and then pass that onto emp_name later in the code.

    thank you for your help!

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    just need clarification about the max number of employees and characters that can be in their name; currently it is
    Code:
    char emp_name[30][10];
    which means that you can have a max of 30 employees each having names no longer than 10 characters, is that right?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by itCbitC View Post
    just need clarification about the max number of employees and characters that can be in their name; currently it is
    Code:
    char emp_name[30][10];
    which means that you can have a max of 30 employees each having names no longer than 10 characters, is that right?
    Each having names no longer than 9 characters (or, if you like, strictly less than 10 characters), since one character must be set aside for \0.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by tabstop View Post
    Each having names no longer than 9 characters (or, if you like, strictly less than 10 characters), since one character must be set aside for \0.
    I thought likewise, however the prompt below contradicts that as in.
    Quote Originally Posted by kisiellll View Post
    Code:
    printf ("Enter the number of employees (1-10): ");
    As it implies that there can be only 10 employess, so I was wondering if the array dimensions were switched around.

Popular pages Recent additions subscribe to a feed

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