Question
C Program Homework Question: The Program is a menu-driven C program based on dynamic memory allocation. On start, this program displays the following menu Add
C Program Homework
Question:
The Program is a menu-driven C program based on dynamic memory allocation. On start, this program displays the following menu
-
Add new student
-
Display all students
-
Delete student by Id
-
Delete student by Name
-
Quit
When option 1 is selected, the program reads student information in struct student and stores it.
When option 2 is selected, the program displays all student information.
When option 3 is selected, the program should read a student Id and search for that id in the stored list of student details. If found, that student record should be deleted.
A similar approach is to be taken for option 4.
When option 5 is selected, the program closes.
With the code attached below, I was able to add new students, display all students, but couldn't delete students by id and by name. Can someone help me with this by fixing the code and printing an output.
#include
struct student { char name[10]; char id[10]; char address[50]; char delete; } ;
struct remove { char delete[10]; } ;
int main()
{ int option = 0, i = 0, l = 0, m = 0, position = 0; struct student *p = malloc(sizeof(struct student)); struct remove *a = malloc (sizeof(struct remove));
printf(".......Student Storage....... "); while (1) { printf("1. Add New Student "); printf("2. Display Student(s) Details "); printf("3. Delete Student by ID "); printf("4. Delete Student by Name "); printf("5. Quit "); printf("...Option... ");
scanf("%d", &option);
switch (option) { case 1: p = realloc(p, sizeof(struct student)*(i+1)); printf("....Enter studentd details.... "); printf("Enter name: "); scanf("%s", p[i].name); printf("Enter ID: "); scanf("%9s", p[i].id); printf("Enter Student Address: "); scanf("%s", p[i].address); printf(" "); i = i + 1; printf(" "); break;
case 2: printf("....Display Student Detail(s).... "); for(l = 0; l < i; l = l + 1) { printf("Student Name: %s ", p[l].name); printf("Student ID: %s ", p[l].id); printf("Student Address: %s ", p[l].address); } printf(" "); break;
case 3: m = i; printf("Select Student ID You Wish to Delete...."); ///// issue with deleting a student by id scanf("%s", &p[l].delete); for(l = m; l>= 0; l--) if(a[l].delete == p[l].id) { position = l; while (position <= m) { p[position]= p[position + 1]; position = position + 1; } l = m --; i --; continue; } printf(" "); break;
case 5: return 0; }
} return 0; }
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