Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  1. Add new student

  2. Display all students

  3. Delete student by Id

  4. Delete student by Name

  5. 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 #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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Describe the power of nonverbal communication

Answered: 1 week ago