Thread: Structures Arrays and Char Strings

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    2

    Thumbs up Structures Arrays and Char Strings

    Hey whats up fellows. I'm taking C programming and Anaytical Geometry over a one month winter semester. Yah its a *****. Anyway the i've been having problems finding examples of the prgram we need to create anywhere on the internet. we're asked to incorporate the following facilitating the use of character strings:

    Accepts ten employee records (structures) from the screen
    employee number
    employee name
    hours worked
    pay rate
    Displays the records to the screen with a gross pay calculated from hours worked and pay rate
    Displays totals for hours worked and gross pay; and averages for hours worked, pay rate, and gross pay
    Sort the records in ascending employee name order.
    More than one function must be used (main, input, output) or (main, input, output, sort).

    but theres no example of this anywhere on the internet or in the C book , trust me i've looked. heres what i have so far but theres complier erros. i'm so stuck please help.

    #include<stdio.h>

    struct work {
    char emp_number[4];
    char name[20];
    int hours[5];
    int rate[4];
    };

    struct work list[10];


    void main(){

    /************
    LOOP TO INPUT THE DATA FOR 10 PEOPLE
    ************/
    {
    int i, emp_hours, emp_rate;
    for(i=0;i<10;i++){

    printf("Enter employee number: ");
    scanf("%s", list[i].emp_number);
    printf("Enter name: ");
    scanf("%s", list[i].name);
    printf("Enter hours worked: ");
    scanf("%d", list[i].hours);
    printf("Enter hourly rate: ");
    scanf("%d", list[i].rate);
    }

    printf("\n");
    printf("EMP# Name Hours Rate\n");
    printf("=== ==== ======= ======\n");
    {
    /*Calculate Gross Pay */
    hours(sum= sum + individual;
    #include<stdio.h>
    }
    }
    whats wrong why wont it let me add the elements of the structure please help

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    1
    sorry dude i've been programming for years and this is over my head. it would take a true genius to figure this out!!!

  3. #3
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    hackerr425 - what is the point of posting a reply to say you dont know the answer???

    corcksmyworld - post the error messages thrown up by the compiler and which compiler you are using.
    Monday - what a way to spend a seventh of your life

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    iain may be able to help you faster than I can, but as for this part of your program I have made a few small changes and it compiles. You still need to work out the sort, and print routines.


    #include<stdio.h>

    struct work {
    char emp_number[4];
    char name[20];
    int hours[5];
    int rate[4];
    };

    struct work list[10];


    int main(){

    /************
    LOOP TO INPUT THE DATA FOR 10 PEOPLE
    ************/
    //{ one too many curly brackets
    int i, emp_hours, emp_rate;
    for(i=0;i<10;i++){

    printf("Enter employee number: ");
    scanf("%s", list[i].emp_number);

    printf("Enter name: ");
    scanf("%s", list[i].name);

    printf("Enter hours worked: ");
    scanf("%d", list[i].hours);

    printf("Enter hourly rate: ");
    scanf("%d", list[i].rate);
    }

    printf("\n");
    printf("EMP# Name Hours Rate\n");
    printf("=== ==== ======= ======\n");
    {
    /*Calculate Gross Pay */
    //hours(sum= sum + individual);
    //#include<stdio.h> I dont know why this is here


    }
    }

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    2

    Thumbs up

    Thanks for taking the time out to help Melvin it means a lot to me. How would you go about totaling the elements of the structure for example totaling list[i].hours since hours is what needs to be totaled and it is a member of the structure??

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Hmm, there are a couple problems here...
    First, your struct itself...
    Code:
    struct work { 
    char emp_number[4]; 
    char name[20]; 
    int hours[5]; 
    int rate[4]; 
    };
    Let's start with emp_number. Is this supposed to actully be a number? Because if so, then you should be using an int, not an array of char. Having it as a char[4] only makes sense if the employee number can be something like.. 4B3
    Why does each employee have 5 different hour values and 4 different rate values?

    Unless you have some special reason for using arrays, change hours to an int, rate to an int, and emp_number to an int. (If you do this, then you'll have to change how you input the emp_number to use scanf("%d", list[i].emp_number);)

    Your assignment states that you need to use an input function, and output function. Your logic seems to all be there, you just need to change it into a function. Basically, your function will look something like this...
    Code:
    void getInput (void)
    {
     for(i=0;i<10;i++){ 
    
      printf("Enter employee number: "); 
      scanf("%s", list[i].emp_number); 
    
      printf("Enter name: "); 
      scanf("%s", list[i].name); 
    
      printf("Enter hours worked: "); 
      scanf("%d", list[i].hours); 
    
      printf("Enter hourly rate: "); 
      scanf("%d", list[i].rate); 
     }
     return; 
    }
    No magic there, just copy n' pasting (see how I suggest changing emp_number to a number above).

    Really, this assignment looks like the kinda thing you can compartmentalize entirely into functions. Here's what my main would look like...
    Code:
    int main ()
    {
     // get 10 employee records from screen.
     getInput();
    
     // display gross pay calculated from hours worked and pay rate 
     displayPayHoursRate();
    
     // Displays totals for hours worked and gross pay; and
     //  averages for hours worked, pay rate, and gross pay
     displayTotalsAverage();
    
     // Sorts the records in ascending employee name order
     sortAscendingName();
    
     // redisplaying the employee info proves you sorted it.
     displayPayHoursRate();
    
     // exit program
     return 0;
    }
    All the work for this program should be hidden in those functions.
    Last edited by QuestionC; 01-20-2002 at 12:00 AM.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 07:05 AM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM
  4. malloc with arrays of strings
    By Lib in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 10:46 PM
  5. arrays of char strings
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 04:46 PM