# Thread: Structures Arrays and Char Strings

1. ## 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>
}
}

2. 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. 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.

4. 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. 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. Hmm, there are a couple problems here...
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.