Question: Hello, my group and I cannot figure out the error in our code. The prompt is shown above. Here is our code: #include #include struct
Hello, my group and I cannot figure out the error in our code. The prompt is shown above.
Here is our code:
#include
#include
struct person {
unsigned int account;
char lastname[15];
char firstname[15];
double age[4];
};
unsigned int menuOptions(void);
void textFile(FILE *readPtr);
void updateRecord(FILE *fPtr);
void newRecord(FILE *fPtr);
void deleteRecord(FILE *fPtr);
int main(void) {
unsigned int choice;
FILE *cfPtr;
if( (cfPtr = fopen("nameage.dat", "rb+")) == NULL) {
puts("File cannot be opened.");
}
else {
while( (choice=menuOptions() )!=5) {
switch (choice) {
case 1: textFile(cfPtr);
break;
case 2: updateRecord(cfPtr);
break;
case 3: newRecord(cfPtr);
break;
case 4: deleteRecord(cfPtr);
break;
default : puts("Wrong choice ");
break;
}
}
fclose(cfPtr);
}
}
void textFile(FILE *readPtr) {
int result;
FILE *writePtr;
struct person pData= { 0,"", "", 0};
if ( (writePtr = fopen("accounts.txt", "w") ) == NULL ){
puts("File cannot be opened. ");
}
else {
rewind(readPtr);
fprintf(writePtr, "%-6s%-16s%-11s%-6s","Account Number", "Last Name", "First Name", "Age");
while(!feof(readPtr)) {
result=fread(&pData, sizeof(struct person),1,readPtr);
if(result!=0 && pData.account!=0) {
fprintf(writePtr, "%-6d%-16s%-11s%-6d", pData.account,pData.lastname, pData.firstname, pData.age);
}
}
}
fclose(writePtr);
}
void updateRecord(FILE *fPtr) {
unsigned int acct;
double newAge;
struct person pData = {0,"", "", 0};
printf("%s", "Enter account of person to update(1-100): ");
scanf("%d", &acct);
fseek(fPtr, (acct-1)*(sizeof (struct person)), SEEK_SET);
fread(&pData, sizeof(struct person),1, fPtr);
if ( pData.account==0) {
printf("Error occured while opening file!");
}
else {
printf("%-6d%-16s%-11s%-6d", pData.account,pData.lastname, pData.firstname, pData.age);
printf("%s", "Enter updated age");
scanf("%d", &newAge);
pData.age+=newAge;
printf("%-6d%-16s%-11s%-6d", pData.account,pData.lastname, pData.firstname, pData.age);
fseek(fPtr, (acct-1) *sizeof(struct person), SEEK_SET);
fwrite(&pData, sizeof(struct person),1, fPtr);
}
}
void deleteRecord(FILE *fPtr) {
struct person pData;
struct person blankPerson = {0, "", "", 0};
unsigned int acctNum;
printf("%s", "Enter account of person to delete(1-100): ");
scanf("%d", &acctNum) ;
fseek(fPtr, (acctNum-1)*sizeof(struct person), SEEK_SET);
fread(&pData, sizeof(struct person),1, fPtr);
if ( pData.account == 0 ){
printf("Error occured while opening file. ");
}
else {
fseek(fPtr, (acctNum -1) * sizeof(struct person), SEEK_SET);
fwrite(&blankPerson, sizeof( struct person), 1,fPtr); }
}
void newRecord(FILE *fPtr) {
struct person pData = {0,"", "", 0};
unsigned int acctNum;
printf("%s","Enter account number: ");
scanf("%d", &acctNum);
fseek(fPtr, (acctNum-1) * sizeof(struct person),SEEK_SET);
fread(&pData, sizeof(struct person),1,fPtr);
if ( pData.account !=0 ) {
printf("Someone already has this account. ");
}
else {
printf("Enter the last name of the person's record to be added. ");
scanf("%s", &pData.lastname);
printf("Enter the first name of the person's record to be added. ");
scanf("%s", &pData.firstname);
printf("Enter the age of the person whose record is being added. ");
scanf("%s", &pData.age);
pData.account=acctNum;
fseek(fPtr, (pData.account-1) *sizeof(struct person), SEEK_SET);
fwrite(&pData, sizeof(struct person),1, fPtr);
}
}
}
unsigned int menuOptions(void) {
unsigned int choice;
printf("Enter choice "
"1- To create a record to text file \accounts.txt\ for printing "
"2- Update an account "
"3- New account "
"4- Delete an account "
"5- end program ? ");
scanf("%u", &choice);
return choice;
I II Write statements that accomplish each of the following. Assume that the structure struct person char lastName 15 J char firstName C 15 char age 4 J; has been defined and that the file is already open for writing. a) Initialize the file "nameage.dat" so that there are 100 records with lastName "unas- signed'', firstname and age "0" b) Input 10 last names, first names and ages, and write them to the file. c) Update a record; if there's no information in the record, tell the user "No info d) Delete a record that has information by reinitializing that particular record. I.12 (Hardware Inventory You're the owner of a hardware store and need to keep an inventory that can tell you what tools you have, how many you have and the cost of each one. Write a program that initializes the file ''hardware. dat" to 100 empty records, lets you input the data concerning each tool, enables you to list all your tools, lets you delete a record for a tool that you no longer have and lets you update any information in the file. The tool identification number should be the record number. Use the following information to start your file: Quantity Cost Record Tool name Electric sander 7 57.98 17 76 11.99 Hammer 21 24 Jig saw 11.00 39 Lawn mower 79.50 56 Power saw 18 99.99 Screwdriver 68 106 6.99 Sledge hammer 11 21.50 83 Wrench 34 7.50
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
