#include <stdio.h>
int fill_array(void);
int display_array(int count);
typedef struct
{
char worker_name[20];
float hourly_pay;
int hours;
}WorkerRecord;
int main()
{
WorkerRecord info[50];
int count, k;
float average_wage;
count = fill_array();
display_array(count);
for(k = 0, average_wage = 0; k < count; k++)
{
average_wage += info[k].hourly_pay;
}
average_wage /= count;
printf("\nAverage Hourly Wage: $%.2f", average_wage);
printf("\n\nPress any key to continue...");
getchar();
return 0;
}
int fill_array(void)
{
WorkerRecord info[50];
int count = 0;
FILE * pointer;
pointer = fopen("workers.txt", "r");
if(pointer == NULL)
{
printf("The file can not be found.\n\n\n");
printf("Press any key to continue...");
getchar();
return 1;
}
while(!feof(pointer))
{
fscanf(pointer, "%s%d%f", info[count].worker_name, &info[count].hours, &info[count].hourly_pay);
count++;
}
fclose(pointer);
return (count);
}
int display_array(int count)
{
WorkerRecord info[50];
int k;
float gross_pay[50];
printf(" Name\t\tHours\t\tHourly Pay\tGross Pay\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for(k = 0; k < count; k++)
{
gross_pay[k] = (info[k].hourly_pay * info[k].hours);
}
for(k = 0; k < count; k++)
{
printf("%s\t\t%d\t\t$%.2f\t\t$%.2f\n", info[k].worker_name, info[k].hours, info[k].hourly_pay, gross_pay[k]);
}
return 0;
}
Ok, I've done everything that I need to do, but I don't understand anything about pointers. I need to be able to access the data stored in the struct in the fill_array function in my other two functions, but I have no clue how to do this. (I only learned about structs the day before last).
Any help is greatly appreciated,
~Alan
Also, if anyone could just explain to me what pointers are and how to use them, that would be great. I've looked up other tutorials online, but they seem so vague that I can't really understand them as I am more of a hands-on learner rather than someone who can read the basics and then know how to do it. ;S