#include<stdio.h>
#define size 21
#define max 10

typedef struct id
 {
  int idnum,q1,q2,q3;
  char fn[size];
  char ln[size];
  char mi[2];
  float ave;
 }id;
  id l[max];
  id a;

int menu()
{
 int i,s=0;
    while(s<1 || s>4)
    {
     clrscr();
     gotoxy(5,5);
     printf("MENU");
     gotoxy(5,7);
     printf("1. insert");
     gotoxy(5,9);
     printf("2. delete");
     gotoxy(5,11);
     printf("3. display");
     gotoxy(5,13);
     printf("4. exit");
     gotoxy(5,15);
     printf("select 1-4: ");scanf("%d",&s);
    }
    return (s);
  }

  void initialize()
  {
   int i;
   for(i=0;i<max;i++);
    l[i].idnum=-1;
  }

  int end()
   {
    int i=0;
    while(l[i].idnum!=-1)
     i++;
    return(i);
   }

  void insert(id a)
  {
   int i,p;
   i=p=0;
   while(a.idnum>l[i].idnum && l[i].idnum!=-1)
      i++;
    if (l[i].idnum==-1)
     {
      l[p].idnum=a.idnum;
      strcpy(l[p].fn,a.fn);
      strcpy(l[p].ln,a.ln);
      strcpy(l[p].mi,a.mi);
      l[p].q1=a.q1;
      l[p].q2=a.q2;
      l[p].q3=a.q3;
      l[p].ave=(float)(a.q1+a.q2+a.q3)/3.0;
     }
    else{
      for(i=end();i>p;i--);
      {
      l[i].idnum=l[i-1].idnum;
      strcpy(l[i].fn,l[i-1].fn);
      strcpy(l[i].ln,l[i-1].ln);
      strcpy(l[i].mi,l[i-1].mi);
      l[i].q1=l[i-1].q1;
      l[i].q2=l[i-1].q2;
      l[i].q3=l[i-1].q3;
      l[i].ave=l[i-1].ave;
      }
      l[p].idnum=a.idnum;
      strcpy(l[p].fn,a.fn);
      strcpy(l[p].ln,a.ln);
      strcpy(l[p].mi,a.mi);
      l[p].q1=a.q1;
      l[p].q2=a.q2;
      l[p].q3=a.q3;
      l[p].ave=(float)(a.q1+a.q2+a.q3)/3.0;
      }
      getch();
    }

    void delete(id a)
    {
     int i,p;
     i=p=0;
     clrscr();
     while(a.idnum!=l[i].idnum && l[i].idnum!=-1)
     i++;
    p=i;
     if(l[i].idnum==-1)
     {
     printf("record not found");
     getch();
         }
     else
     for(i=p;i<end();i++)
     {
      l[i].idnum=l[i+1].idnum;
      strcpy(l[i].fn,l[i+1].fn);
      strcpy(l[i].ln,l[i+1].ln);
      strcpy(l[i].mi,l[i+1].mi);
      l[i].q1=l[i+1].q1;
      l[i].q2=l[i+1].q2;
      l[i].q3=l[i+1].q3;
      l[i].ave=l[i+1].ave;
      }

    }
  void displayall()
  {
  int i;
  printf("idnum    1stnme    lastnme     midtial       q1      q2      q3    average");
  for(i=0;i<end();i++)
  {
    gotoxy(1,i+3);
    printf("%d",l[i].idnum);
    gotoxy(10,i+3);
    printf("%s",l[i].fn);
    gotoxy(20,i+3);
    printf("%s",l[i].ln);
    gotoxy(32,i+3);
    printf("%s",l[i].mi);
    gotoxy(46,i+3);
    printf("%d",l[i].q1);
    gotoxy(55,i+3);
    printf("%d",l[i].q2);
    gotoxy(63,i+3);
    printf("%d",l[i].q3);
    gotoxy(68,i+3);
    printf("%.2f",l[i].ave);
    }
    getch();
 }

 main()
 {
 int i;
 id b;
 initialize();
 while(1)
 {
 i=menu();
 if(i==1)
  { clrscr();
    printf("\n input id number: "); scanf("%d",&b.idnum);
    printf("input firstname: "); scanf("%s",b.fn);
    printf("input lastname: "); scanf("%s",b.ln);
    printf("input mid initial: "); scanf("%s",b.mi);
    printf("input quiz1:");scanf("%d",&b.q1);
    printf("input quiz2:");scanf("%d",&b.q2);
    printf("input quiz3:");scanf("%d",&b.q3);
    insert(b);}
 else
   if(i==2) {
    clrscr();printf("input id number:");scanf("%d",&b.idnum);
    delete(b);  }
 else
   if(i==3){
   clrscr();displayall();}
 else
   exit(0);

 getch();
 }
}

Dani AI

Generated

The symptom reported by — the first record being overwritten when a second is inserted — points to logic errors rather than a runtime quirk. was right to flag parameter passing and name shadowing, but there are several additional, concrete bugs that typically cause exactly this behaviour: stray semicolons after loop headers, an initialization loop that never runs, incorrect use of the insertion index (writing to index zero every time), and missing bounds checks when shifting records.

Fix checklist (apply in order)

  • Ensure the array is properly initialized: run a loop that actually executes and mark every slot as “empty” (use a sentinel such as a negative id). A stray semicolon on the loop header makes the body run only once after the loop ends, which leaves the array uninitialized.
  • Make the end/length routine robust: stop when the sentinel is found or when the array limit is reached. That prevents overruns when the table is full.
  • Rework insertion: find the correct insertion index by scanning until an empty slot or a greater-or-equal id. If insertion requires making space, shift elements right from the current last element to the insertion index (iterate downwards). Avoid using a separate confusing variable that stays zero; use a clear name (e.g. newIndex or pos). Remove any accidental semicolons after for/while headers so loops actually move data.
  • Rework deletion symmetrically: find the item, shift subsequent entries left, and mark the now-free last slot as empty.

Other important notes

  • Don’t shadow globals: rename the function parameter (avoid using the same name as the global a). Passing a pointer is fine, but not required if the function copies the struct correctly; the clarity of names matters more here.
  • Watch string buffers: middle initial storage and scanf usage look risky; either make the middle-initial field big enough or read a single character safely, and always limit scanf to buffer size.
  • Compile with warnings enabled (e.g. -Wall -Wextra) and test stepwise: initialize → insert one record → inspect internal array → insert more → display → delete → display. That sequence quickly reveals off-by-one and overwrite bugs.

Acknowledgement: ’s advice on avoiding pass-by-value hiding and better names is useful; combined with the loop/shift fixes above the program will retain all records and display them correctly.

Recommended Answers

All 2 Replies

help! my program is about struct!! it runs but i have a difficulty in displaying all the inserted data! it display the first data inputted by the user but when you insert another data, the 1st inputted data is deleted and the second data is being displayed.
how can i display all the data?
pls help me!!

your program should be passing the structure aboud to other functions by reference instead of by value so that any changes made by the function will be made to the original copy of the structure. When you pass a structure by value the program must make a copy of the sturcutre and pass the copy. Here is an example of how the receiving function should declare the parameter -- not the asterisk.

void insert(id* a)
{
...
<snip>
while(a->idnum > l[i].idnum && l[i].idnum != -1)

You should try to use more descriptibe variable names -- variables a and l are confusing.

Parameter name a in the insert function duplicates and hides the global variable also with the same name. rename the parameter in the insert function to something else.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.