Code:
#include <stdio.h>
int main( void )
{
int request;
int employee;
double payrate;
double hours;
char name[ 5 ];
FILE *cfPtr;
if ( ( cfPtr = fopen( "payroll.dat", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
system("pause");
}
else {
printf( "Enter request\n"
" 1 - Show all records\n"
" 2 - Show all of the records in which the gross pay is equal to or less than 400.00\n"
" 3 - Show all of the records in which gross pay is greater than 400.00\n"
" 4 - End of run\n? " );
scanf( "%d", &request );
while ( request != 4 ) {
fscanf( cfPtr, "%s %d %4lf %4lf", name, &employee, &hours, &payrate );
switch ( request ) {
case 1:
printf( "\nShow all of the records:\n" );
printf("%-9s %-15s %-17s %-17s %s\n",
"Name", "employee #", "hours worked", "rate of pay", "gross pay");
while ( !feof( cfPtr ) ) {
printf( "%-5s %11d %16.2f %17.2f %17.2f\n",
name, employee, hours, payrate, payrate * hours );
fscanf( cfPtr, "%s%d%lf%lf", name, &employee, &hours, &payrate );
}
break;
case 2:
printf( "\nShow all of the records in which the gross pay is equal to or less than 400.00\n" );
printf("%-9s %-15s %-17s %-17s %s\n",
"Name", "employee #", "hours worked", "rate of pay", "gross pay");
while ( !feof( cfPtr ) ) {
if ( payrate * hours <= 400.00 ) {
printf( "%-5s %11d %16.2f %17.2f %17.2f\n",
name, employee, hours, payrate, payrate * hours );
}
fscanf( cfPtr, "%s%d%lf%lf", name, &employee, &hours, &payrate );
}
break;
case 3:
printf( "\nShow all of the records in which gross pay is greater than 400.00\n" );
printf("%-9s %-15s %-17s %-17s %s\n",
"Name", "employee #", "hours worked", "rate of pay", "gross pay");
while ( !feof( cfPtr ) ) {
if ( payrate * hours > 400.00 ) {
printf( "%-5s %11d %16.2f %17.2f %17.2f\n",
name, employee, hours, payrate, payrate * hours );
}
fscanf( cfPtr, "%s%d%lf%lf", name, &employee, &hours, &payrate );
}
break;
}
rewind( cfPtr );
printf( "\n? " );
scanf( "%d", &request );
}
printf( "End of run.\n" );
fclose( cfPtr );
}
return 0;
}
i can't figure out if i'm doing something wrong but according to the instructions, the test data in the data file should appear as: