Question
C Language programming problem. Hello, for some reason I have an infinite loop in the update function. I dont know how to fix it. Can
C Language programming problem.
Hello, for some reason I have an infinite loop in the update function. I dont know how to fix it. Can you help show me other errors?
Prompt:
CODE:
#include
struct person { char lastName[15]; char firstName[15]; char age[4]; };
int menu (); void initialize (); void addTenRecords (); void updateRecord (); void deleteRecord (); void showContent ();
int main (void) { setbuf (stdout, NULL);
int choice; while ((choice = menu ()) != 5) { switch (choice) { case 1: initialize (); break; case 2: addTenRecords (); break; case 3: updateRecord (); break; case 4: deleteRecord (); break; default: printf ("Wrong Choice entered "); break; } }
return EXIT_SUCCESS; } int menu () { const int MAX_CHOICES = 5; const int MIN_CHOICES = 0; int choice; while (choice > MAX_CHOICES || choice
void initialize () { FILE *ptrInitial;
struct person personData = { "unassigned", "", "0" };
fflush (stdin);
if ((ptrInitial = fopen ("nameage.dat", "w")) == NULL) { printf ("Error "); } else { size_t i; for (i = 0; i
showContent (); } void addTenRecords () { FILE *ptr;
struct person personData;
if ((ptr = fopen ("nameage.dat", "a")) == NULL) { printf ("Error "); } else { size_t i; for (i = 0; i
{ printf (" Please enter first name: "); scanf ("%s", personData.firstName);
printf (" Please enter last name: "); scanf ("%s", personData.lastName);
printf (" Please enter age: "); scanf ("%s", personData.age);
fwrite (&personData, sizeof(personData), 1, ptr);
} fclose (ptr); } showContent (); } void showContent () { FILE *ptrRead;
struct person personData;
if ((ptrRead = fopen ("nameage.dat", "r")) == NULL) { printf ("Error "); } else { while ((fread (&personData, sizeof(personData), 1, ptrRead))) { printf ("%-10s%-10s%-7s ", personData.firstName, personData.lastName, personData.age); }
}
fclose (ptrRead); } void updateRecord () { FILE *ptrUpdate;
char lastName[15]; struct person personData;
if ((ptrUpdate = fopen ("nameage.dat", "r+")) == NULL) { printf ("Error "); } else { printf ("Enter the last Name of the record you want to update: "); scanf ("%s", lastName);
while ((fread (&personData, sizeof(personData), 1, ptrUpdate))) { if (strcmp (personData.lastName, lastName) == 0) { printf (" Enter first name: "); scanf ("%s", personData.firstName);
printf (" Enter last name: "); scanf ("%s", personData.lastName);
printf (" Enter age: "); scanf ("%s", personData.age);
fseek (ptrUpdate, ((-1) * (sizeof(personData))), SEEK_CUR);
fwrite (&personData, sizeof(personData), 1, ptrUpdate);
} } fclose (ptrUpdate); } showContent (); } void deleteRecord () { FILE *ptrDelete;
char lastName[15]; struct person personData;
if ((ptrDelete = fopen ("nameage.dat", "r+")) == NULL) { printf ("Error "); } else { printf ("Enter the last name: "); scanf ("%s", lastName);
while ((fread (&personData, sizeof(personData), 1, ptrDelete))) { if (strcmp (personData.lastName, lastName) == 0) { strcpy (personData.lastName, "unassigned");
strcpy (personData.firstName, "");
strcpy (personData.age, "0");
fseek (ptrDelete, ((-1) * (sizeof(personData))), SEEK_CUR);
fwrite (&personData, sizeof(personData), 1, ptrDelete);
} } fclose (ptrDelete); } showContent (); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started