From: Ejjman
Both text files are already in ascending order that I receive.
I kinda see what you are saying, how do I manipulate that result file? Like, if there's a number that is smaller, how do I place it between two already placed numbers? Also, how do I omit one?
Thx for the post.
Not sure if this was ever solved, but I have an old source file that may be of help. It shows how to read and write to a file. If you have any further questions ask. (I'm not extremely fluent in C, but I know some.) :P
EDIT: WOW, I don't know how I missed the "not able to use arrays" part. If that's the case, you will just need to use several different variables and several loops, as the for loop I use needs the array.
Example
#include <stdio.h>
int fill_array(void);
int display_array(int count);
typedef struct
{
char worker_name[20];
float hourly_pay;
int hours;
}WorkerRecord;
WorkerRecord info[50];
int main()
{
int count, k;
float average_wage, total_wage = 0, total_hourly = 0;
float gross_pay[50];
FILE * ptr;
count = fill_array();
display_array(count);
for(k = 0; k < count; k++)
{
total_hourly += info[k].hourly_pay;
}
average_wage = total_hourly /count;
printf("\nAverage Hourly Wage: $%.2f\n\n", average_wage);
for(k = 0; k < count; k++)
{
gross_pay[k] = (info[k].hourly_pay * info[k].hours);
total_wage += gross_pay[k];
}
printf("This Week's Total Payroll: $%.2f", total_wage);
ptr = fopen("report.txt", "w");
fprintf(ptr, " Name\t\tHours\t\tHourly Pay\tGross Pay\n");
fprintf(ptr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for(k = 0; k < count; k++)
{
fprintf(ptr, "%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]);
}
fprintf(ptr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
fprintf(ptr, "\nAverage Hourly Wage: $%.2f\n\n", average_wage);
fprintf(ptr, "This Week's Total Payroll: $%.2f", total_wage);
fclose(ptr);
printf("\n\nPress any key to continue...");
while(!getchar());
return 0;
}
int fill_array(void)
{
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)
{
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]);
}
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
return 0;
}
Yes I know you didn't need all of it, but just in case you wanted to test it.
Input File
Bob 40 10
Sean 35 12
Linda 30 15
Michael 45 9
Kelly 25 11
(Wow, code tags don't like tabs or using 5 spaces.)