Grumpy,
To play devils advocate, if everyone who programmed wrote fault-free code then would there not be a lot less programmers. That being said, I do want to be in the group of "good" programmers at some point. It is difficult because, my class doesn't even attempt to convey portability or stress good habits even when the bad ones produce the same result in that particular assignment. Granted this is the class which comes directly after the intro class. The assignment I have written, will "sadly" get me a 100 but only because it does everything the instructor wanted it to do. I could just stop there but I want it to be sound. I am still at the infant stage of writing/designing but I understand the importance of good fundamentals.
Here is the two structs that my program uses. I do have a EMPLOYEE person[10000];
Code:
typedef struct {
int month;
int day;
int year;
}DATE;
typedef struct {
char name[20];
int age;
float hourlyWage;
float hoursWkd;
float otHoursWkd;
float regPay;//computed
float otPay;//computed
float totalPay;//computed
DATE payDate; //this is a struct inside a struct
}EMPLOYEE;
and here is the function where the person arrays are populated and written to file
Code:
char sValidate[5] = "exit";
FILE *f;
int count = 0, i = 0;
f = fopen("employeeRecords.bin", "ab");
while (strcmp( sValidate, person[i].name)) {
printf("Please enter name or type exit to return to main menu: ");
scanf("%s", person[i].name); //must use the '->' when passing by by refrence, must use '&' sign
flush;
if (!strcmp( sValidate, person[i].name))
break;
printf("\nPlease enter age of %s: ", person[i].name);
scanf("%i", &person[i].age);
flush;
printf("\nPlease enter the hourlyWage for %s: ", person[i].name);
scanf("%f", &person[i].hourlyWage);
flush;
printf("\nPlease enter the hours worked for %s: ", person[i].name);
scanf("%f", &person[i].hoursWkd);
if (person[i].hoursWkd > 40) {
person[i].regPay = person[i].hourlyWage * 40.0;
person[i].otHoursWkd = person[i].hoursWkd - 40.0;
person[i].otPay = person[i].otHoursWkd * (person[i].hourlyWage * 1.5);
person[i].totalPay = person[i].regPay + person[i].otPay;
}
else {
person[i].totalPay = person[i].hoursWkd * person[i].hourlyWage;
person[i].regPay = person[i].totalPay;
person[i].otHoursWkd = 0.0; person[i].otPay = 0.0;
}
flush;
printf("\nEnter 2 digit month: ");
scanf("%i", &person[i].payDate.month); //must use the '->' when passing by by refrence, must use '&' sign
flush;
printf("\nEnter 2 digit day: ");
scanf("%i", &person[i].payDate.day); //must use the '->' when passing by by refrence, must use '&' sign
flush;
printf("\nEnter 4 digit year: ");
scanf("%i", &person[i].payDate.year); //must use the '->' when passing by by refrence, must use '&' sign
flush;
fwrite(&person[i], sizeof(person[i]), 1, f);
i++;
}
fclose(f);
}//end function loadPayRoll
I know this is probably not sound code, especially my exit implementation, but most of the sloppiness of my past assignments are not present in this one and the plan is to look back from future programs and know that a lot of issues above are not present in future programs.
Thanks,
Mike