I am a new user on this C and have a hard trouble with I/O's. If you could let me know what I am not clarifying. Thank You!
#include <stdio.h>
#include <stdlib.h>
// employee structure definition //
struct year2009Data {
float SSN; // employee social security number //
char lastName [25]; // employee last name //
char firstName [15]; // employee first name //
int W4with; // employee elected withholding //
float hourlywage; // employee hourly wage //
float hours; // employee weekly hours //
float gross; // gross wages for week //
float fedwith; // federal tax for week //
float sstax; // social security tax for week //
float medtax; // medicare tax for week //
float statetax; // state tax for week //
float countytax; // county tax for week //
float net; // net pay for week //
float ytdgross; // ytd gross wages //
float ytdfedwith; // ytd federal withholding tax //
float ytdsstax; // ytd social security tax //
float ytdmedtax; // ytd medicare tax //
float ytdstatetax; // ytd state tax //
float ytdcountytax; // ytd county tax //
float ytdnet; // ytd netpay //
}; // End structure //
void employeeHours (FILE *fPtr);
void editEmployee (FILE *fPtr);
void newEmployee (FILE *fPtr);
void reportFile (FILE *readPtr);
int main (void)
{
FILE *cfPtr; // year2009.dat file pointer //
int choice; // user's choice //
int employeenum; // employee number //
int currentornew; //current or new employee
int newemployee; //new employee format
int endLoop = 0; //starting the end loop at 0 so it is not trash
// prompy user for choice //
while( endLoop == 0) {
printf(" Welcome to ESLR, LLC \n"
"1-Enter Employee Hours \n"
"2-Edit Employee Information \n"
"3-Add New Employee \n"
"4-Creat A Report \n"
"5-Exit Program \n"); /*Prompt for input*/
scanf( "%d", ¤tornew);
if ((currentornew > 5) || (currentornew < 1)){
system("cls");
printf("Invalid entry, please try again.\n\n");
} //end if statement
else {
endLoop = 1;
} //end else statement
} //end while statement
// opens file or exits if file cannot be opened //
if (( cfPtr = fopen("year2009Data.dat", "w+")) == NULL) {
printf ("File could not be opened.\n");
} // end if //
switch ( currentornew ) {
//update employee
case 1:
employeeHours (cfptr);
break;
//enter employee hours
case 2:
editEmployee (cfptr);
break;
//edit employee information
case 3:
newEmployee (cfptr);
break;
//add new employee
case 4:
reportfile (cptr);
//Summary
break;
case 5:
//Exit the program
default:
printf( "Incorrect Choice\n");
break;
} //succesful programming
}
fclose (cfPtr); // closes year2009.dat file //
} // end else //
return 0; // indicates successful program //
} // end main //
// Edit employee function //
void editEmployee (FILE *fPtr)
{
int employeenum;
struct year2009Data Employee = {0};
// ask user for employee number //
printf("Enter employee number: ");
scanf("%d", &employeenum);
// move file pointer to correct location //
fseek(fPtr, (employeenum - 1) * sizeof( struct year2009Data), SEEK_SET);
// read record from file //
fread(&employeenum, sizeof( struct year2009Data), 1, fPtr);
// error displayed if employee number has already been used //
if (employeenum == 0) {
printf("Employee number has no information.\n, year2009.employeenum");
} // end if //
else { // edit employee information//
// prompt user for employee information //
printf("Enter employee social security number: ");
scanf("%lf", Employee.SSN);
printf("Enter employee last name: ");
scanf("%s", Employee.lastName);
printf("Enter employee first name: ");
scanf("%s", Employee.firstName);
printf("Enter value from box XX of employee W4: ");
scanf("%d", Employee.W4with);
printf("Enter employee hourly wage: ");
scanf("%lf", Employee.hourlywage);
Employee.employeenum = employeenum;
// move file pointer to correct record in file //
fseek(fPtr, (employeenum - 1) * sizeof( struct year2009Data), SEEK_SET);
// write new data to file //
fwrite(&employeenum, sizeof( struct year2009Data), 1, fPtr);
} // end else //
} // end function editEmployee //
// Update employee hours for week //
void employeeHours (FILE *fPtr)
{
int employeenum, hours;
struct year2009Data Employee = {0};
// obtain employee number to update //
printf("Enter employee number to update: ");
scanf("%d", &employeenum);
// move file pointer to correct location //
fseek(fPtr, (employeenum - 1) * sizeof( struct year2009Data), SEEK_SET);
// read record from file //
fread(&employeenum, sizeof( struct year2009Data), 1, fPtr);
// error notice if employee cannot be found //
if (Employee.employeenum == 0) {
printf("Employee #%d has no information. \n", employeenum);
} // end if //
// update employee record //
else {
printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", "employeenum", "SSN", "lastName",
"firstName", "W4with", "hourlywage", "hours", "gross", "fedwith", "sstax", "medtax",
"statetax", "countytax", "net", "ytdgross", "ytdfedwith", "ytdsstax", "ytdmedtax",
"ytdstatetax", "ytdcountytax", "ytdnet");
// request hours //
printf("Please input hours for the week:");
scanf("%lf", &hours);
// calculate taxes and wages //
Employee.gross = Employee.hours * Employee.hourlywage;
Employee.fedwith = Employee.gross * 0.20;
Employee.sstax = Employee.gross * 0.062;
Employee.medtax = Employee.gross * 0.0145;
Employee.statetax = Employee.gross * 0.034;
Employee.countytax = Employee.gross * 0.044;
Employee.net = Employee.gross - Employee.fedwith - Employee.sstax - Employee.medtax - Employee.statetax - Employee.countytax;
Employee.ytdgross = Employee.ytdgross + Employee.gross;
Employee.ytdfedwith = Employee.ytdfedwith + Employee.fedwith;
Employee.ytdsstax = Employee.ytdsstax + Employee.sstax;
Employee.ytdmedtax = Employee.ytdmedtax + Employee.medtax;
Employee.ytdstatetax = Employee.ytdstatetax + Employee.statetax;
Employee.ytdcountytax = Employee.ytdcountytax + Employee.countytax;
Employee.ytdnet = Employee.ytdnet + Employee.net;
// move file pointer to correct record in file //
fseek(fPtr, (employeenum - 1) * sizeof( struct year2009Data), SEEK_SET);
// write new data to file //
fwrite(&employeenum, sizeof( struct year2009Data), 1, fPtr);
} // end else //
} // end function employeeHours //
// New employee function //
void newEmployee (FILE *fPtr)
{
FILE *cfPtr; // year2009Data file pointer //
// create year2009Data with default info //
struct year2009Data blankEmployee = {0, 000000000, "", ""};
int employeenum;
if ((cfPtr = fopen("year2009Data", "a+")) == NULL) {
printf("File could not be opened.\n");
} // end if //
else {
// ask user for new employee number //
printf("Enter new employee number: ");
scanf("%d", &employeenum);
while (blankEmployee.employeenum !=0) {
// move file pointer to correct location //
fseek(fPtr, (employeenum - 1) * sizeof( struct year2009Data), SEEK_SET);
// read record from file //
fread(&employeenum, sizeof( struct year2009Data), 1, fPtr);
// error displayed if employee number has already been used //
if (blankEmployee.employeenum !=0) {
printf("Employee number #%d has already been used.\n", blankEmployee.employeenum);
} // end if //
else {
// create new employee //
// prompt user for employee information //
printf("Enter employee social security number: ");
scanf("%lf", &blankEmployee.SSN);
printf("Enter employee last name: ");
scanf("%s", &blankEmployee.lastName);
printf("Enter employee first name: ");
scanf("%s", &blankEmployee.firstName);
printf("Enter value from box XX of employee W4: ");
scanf("%d", &blankEmployee.W4with);
printf("Enter employee hourly wage: ");
scanf("%lf", &blankEmployee.hourlywage);
blankEmployee.employeenum = employeenum;
// move file pointer to correct record in file //
fseek(fPtr, (employeenum - 1) * sizeof( struct year2009Data), SEEK_SET);
// write new data to file //
fwrite(&employeenum, sizeof( struct year2009Data), 1, fPtr);
} // end else //
} // end while //
} // end else //
} // end function newEmployee //
// Create .txt file report //
void reportFile (FILE *readPtr)
{
FILE *writePtr; // year2009.txt file pointer //
// creates year2009 file structure with blank data //
struct year2009Data Employee = { 0, 000000000, "", ""};
// fopen opens the file; exits if the file cannot be opened //
if ((writePtr = fopen("year2009.txt", "r")) == NULL) {
printf ("File could not be opened.\n");
} // end if //
else {
rewind (readPtr); // sets pointer back to beginning of file //
fprintf (writePtr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", "employeenum", "SSN", "lastName",
"firstName", "W4with", "hourlywage", "hours", "gross", "fedwith", "sstax", "medtax",
"statetax", "countytax", "net", "ytdgross", "ytdfedwith", "ytdsstax", "ytdmedtax",
"ytdstatetax", "ytdcountytax", "ytdnet");
// copy all files from random-access file to text file //
while (!feof(readPtr)) {
fread (&Employee, sizeof( struct year2009Data), 1, readPtr);
// creates single employee record to file //
if (Employee.employeenum !=0) {
fprintf (writePtr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", "employeenum", "SSN", "lastName",
"firstName", "W4with", "hourlywage", "hours", "gross", "fedwith", "sstax", "medtax",
"statetax", "countytax", "net", "ytdgross", "ytdfedwith", "ytdsstax", "ytdmedtax",
"ytdstatetax", "ytdcountytax", "ytdnet");
} // end if //
} // end while //
} // end else //
} // end function //