#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();
 }
}

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.

This article has been dead for over six months. Start a new discussion instead.