Arrays

This is a discussion on Arrays within the C Programming forums, part of the General Programming Boards category; Trying to pass the array to the 2 different functions. having trouble. PLEASE HELP!!!!!! After it displays the menu and ...

  1. #1
    Unregistered
    Guest

    Question Arrays

    Trying to pass the array to the 2 different functions. having trouble. PLEASE HELP!!!!!!
    After it displays the menu and you make the selection it kicks out to the debugger.


    #include<stdio.h>
    void displaymenu(void);
    int empselection();
    int gethoursworked(char);
    int getpayrate();

    int main()
    {
    int sel;
    char emp[5][15] ={"","Joe Smith", "Jane Smith", "Jane Doe", "Chuck Miller"};
    int grosspay;
    double netpay;
    int hours;
    int rate;

    displaymenu();
    sel = empselection();

    if(sel == 0)
    return 0;
    else if (sel == 1)
    emp[1][15];
    else if (sel == 2)
    emp[2][15];
    else if (sel == 3)
    emp[3][15];
    else if (sel == 4)
    emp[4][15];
    else
    puts("\a\n\nINVALID SELECTION!!!!!\n\n"), main();
    hours = gethoursworked(emp[sel][15]);
    rate = getpayrate();
    grosspay = hours * rate;
    netpay = (grosspay * .79);
    if (hours ==1)
    printf("For %d hour, They will make $%6.2f.\n", hours, netpay);
    else
    printf("For %d hours, They will make $%6.2f.\n", hours, netpay);
    int main();
    return 0;}
    int gethoursworked(char emp)
    {
    int hours;
    printf("Enter hours worked for %s: ", emp);
    scanf("%d", &hours);
    return hours;
    }
    empselection()
    {
    int sel;
    printf("Enter a selection: ");
    scanf("%d", &sel);
    return sel;
    }

    int getpayrate()
    {
    int rate;
    printf("What is their pay rate? ");
    scanf("%d", &rate);
    return rate;
    }

    void displaymenu()
    {
    puts("Enter 0 to quit");
    puts("What Employee would you like to Calculate Pay for?");
    puts("1: Joe Smith");
    puts("2: Jane Smith");
    puts("3: John Doe");
    puts("4: Chuck Miller");
    return;}

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    16

    Answer

    You can do something like this.

    int *function(char *array);

    int main()
    {
    char fname[20];

    printf("Please Enter Your First Name: ");
    scanf("%s",&fname);

    char *ptr;
    ptr = fname;

    YourFunction(ptr);
    }

    int *YourFunction(char *array)
    {
    ....do this
    return a_pointer;
    }


    I hope this helps
    pr0ficient@hotmail.com

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Various problems with parameter passing...
    Code:
    #include<stdio.h> 
    #define MAX_EMP 5
    
    // this function is passed the whole emp array
    void displaymenu(char emp[MAX_EMP][15] ); 
    int empselection(); 
    
    // this function is passed just one employee
    int gethoursworked(char emp[]); 
    int getpayrate(); 
    
    int main() 
    { 
        int sel; 
        char emp[MAX_EMP][15] = {
            "","Joe Smith", "Jane Smith", "Jane Doe", "Chuck Miller"}; 
        int grosspay; 
        double netpay; 
        int hours; 
        int rate; 
    
        displaymenu(emp); 
        sel = empselection(); 
        // do your error checking
        hours = gethoursworked( emp[sel] );
        rate = getpayrate(); 
        grosspay = hours * rate; 
        netpay = (grosspay * .79); 
        if (hours ==1 ) 
            printf("For %d hour, They will make $%6.2f.\n", hours, netpay); 
        else 
            printf("For %d hours, They will make $%6.2f.\n", hours, netpay); 
        return 0;
    } 
    
    int gethoursworked(char emp[]) 
    { 
        int hours; 
        printf("Enter hours worked for %s: ", emp); 
        scanf("%d", &hours); 
        return hours; 
    } 
    
    int empselection() 
    { 
        int sel; 
        printf("Enter a selection: "); 
        scanf("%d", &sel); 
        return sel; 
    } 
    
    int getpayrate() 
    { 
        int rate; 
        printf("What is their pay rate? "); 
        scanf("%d", &rate); 
        return rate; 
    } 
    
    void displaymenu(char emp[MAX_EMP][15]) 
    { 
        int i;
        puts("Enter 0 to quit"); 
        puts("What Employee would you like to Calculate Pay for?"); 
        for ( i = 1 ; i < MAX_EMP ; i++ ) {
            printf( "%d: %s\n", i, emp[i] );
        }
        return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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