0

please help me in this code please remove all these errors because i did not remove these errors due to beginner in c

Attachments
#include<graphics.h>
#include<dos.h>
#include<alloc.h>
#include<stdio.h>
#include<string.h>

union REGS i,o;
char *menu[]={"1.Add Records","2.Show Records","3.Modify Records",
	   "4.Del Records","5.Search Record","6.Exit"};


main()
{
  int gd=DETECT,gm,choice=1,width=0,i,count,sr=0;
  char **buffer;
   FILE *m,*t;
  char sname[20];
  long int size;
  char mrec;
	 struct cust
	{
	  char name[20];
	  int accno;
	  float balance;
	};
  struct cust s;

  m=fopen("C:\record.txt","rb+");

  if(m==NULL)
  {
	t=fopen("C:\record.txt","wb+");

	if(t==NULL)
	{ printf("Error in opening file ");
	  exit();
	}
   }
  size=sizeof(s);
  initgraph(&gd,&gm,"C:\Tc\Bgi");

  setbkcolor(1);
  setcolor(11);
  if(initmouse()==0)
  {
   printf("Mouse driver not loaded...");
   exit();
  }

  count=sizeof(menu)/sizeof(char *);
  settextstyle(TRIPLEX_FONT,0,3);
  displaymenu(menu,count,10,10);

  for(i=0;i<count;i++)
  {
   if(textwidth(menu[i])>width)
	 width=textwidth(menu[i]);
  }

//  movemouseptr(&x1,&y1);

  buffer=malloc(sizeof(menu));
  savemenu(menu,buffer,width,count,10,10);
  showmouseptr();
  while (1)
  {   displaymenu(menu,count,10,10);
	choice=getresponse(menu,buffer,width,count,10,10);
	gotoxy(50,15);

	 hidemouseptr();
	 switch (choice)
   {
	case 1 : cleardevice();
	 fseek(m,0,SEEK_END);
	 mrec='y';
	 gotoxy(1,1);
	 while(mrec=='y')
	 {
	   printf("Enter the Name of the Custumer ");
	   scanf("%s",s.name);
	   printf("Enter the Acc.no of the Custumer ");
	   scanf("%d",&s.accno);
	   printf("Enter the Balance of the Custumer ");
	   scanf("%f",&s.balance);

	   fwrite(&s,size,1,m);
	   printf("Are there more Records (Y/N)");
	   fflush(stdin);
	   mrec=getche();
	 }    printf("Press any key to go back to menu");
	 cleardevice();
	 break;

	case 2:  cleardevice();
	 rewind(m);
	   gotoxy(1,1);
	   printf("			Name		Acc.No		Ammount Due");
	   sr=0;
	  if(fread(&s,size,1,m)==1)
	 { rewind(m);
	   while(fread(&s,size,1,m)==1)
	   {
	 printf("%30s%12d%23.2f",s.name,s.accno,s.balance);
	 sr++;
	   }
	 }  else
	printf("			There is no record to Display");
	   printf("		There are %d Records",sr);
	   printf("Press any key to go back to menu");
	 getch();
	 cleardevice();
	 break;

	case 3:  clearviewport();
	 mrec='y';
	  gotoxy(1,1);
	  while(mrec=='y')
	  {
	  printf("Enter the name of Customer whose rec. is to be modified");
	  scanf("%s",sname);
	   // gets(sname);
	  rewind(m);
	  while (fread(&s,size,1,m)==1)
	  { sr=0;
	 if( strcmp (s.name,sname)==0)
	 { sr=1;
	//   printf("Enter new name,Acc.no,Balance ");
	//   scanf("%s%d%f",s.name,&s.rollno,&s.percent);
	   printf("Enter the new Name of the Custumer ");
	   scanf("%s",s.name);
	 //	   gets(s.name);

	   printf("Enter the new Acc.no of the Custumer ");
	   scanf("%d",&s.accno);
	   printf("Enter the new Balance of the Custumer ");
	   scanf("%f",&s.balance);




	   fseek(m,-size,SEEK_CUR);
	   fwrite(&s,size,1,m);
	   break;
	 }
	  }
	  if(sr==0)
	printf("	Record  is not found",sname);
	  printf("Modify more records (Y/N)");
	  fflush(stdin);
	  mrec=getche();

	   }
	clearviewport();
	break;
	case 4: clearviewport();
	 mrec='y';
	 gotoxy(1,1);
	 while(mrec=='y')
	 {
	 printf("Enter the name of the Customer to delete the rec ");
	 scanf("%s",sname);
	   //	   gets(sname);
	 t=fopen("C:\stemp.txt","wb");

	 rewind (m);
	while(fread(&s,size,1,m)==1)
	{
	  if(strcmp(s.name,sname)!=0)
	fwrite(&s,size,1,t);
	}
	 fclose(m);
	 fclose(t);
	 remove("C:\record.txt");
	 rename("C:\stemp.txt","C:\record.txt");

	 m=fopen("C:\record.txt","rb+");

	 printf("Delete more records (Y/N)");
	 fflush(stdin);
	 mrec=getche();
	  }  clearviewport();
	// displaymenu(menu,count,10,10);
	// getch();
	// choice=getresponse(menu,buffer,width,count,10,10);
	 break;
	case 5:  clearviewport();
	 mrec='y';
	  gotoxy(1,1);
	  while(mrec=='y')
	  {
	  printf("Enter the name of Customer whose rec. is to be Searched");
	  scanf("%s",sname);
	//  gets(sname);
	  rewind(m);
	  while (fread(&s,size,1,m)==1)
	  {  sr=0;
	 if( strcmp (s.name,sname)==0)
	 {
	 //  printf("Enter new name,Acc.no,Balance");
	 //  scanf("%s%d%f",s.name,&s.rollno,&s.percent);
	//	fseek(m,-size,SEEK_CUR);
	//   fwrite(&s,size,1,m);
	  printf("Record found");
	  printf("			  Name		Acc.No		Ammount Due");
	  printf("%30s%12d%23.2f",s.name,s.accno,s.balance);
	  sr=1;
	  getch();
	  break;
	 }
	  }
	  if(sr==0)
	   printf("	Record  is not found",sname);
	  printf("Search more records (Y/N)");
	  fflush(stdin);
	  mrec=getche();

	   }
	clearviewport();
	break;

	case 6:
	 fclose(m);
	 exit();

   }
  showmouseptr();
  }
}

displaymenu(char **menu,int count,int x1,int y1)
{
 int i,h;

 h=textheight(menu[0]);

 for(i=0;i<count;i++)
   outtextxy(x1,y1+i*(h+5),menu[i]);
}

savemenu (char **menu,char **buffer,int width,int count,int x1,int y1)
{
 int i,x2,yy1,yy2,area,h;

 h=textheight(menu[0]);

 for(i=0;i<count;i++)
 {
   x2=x1+width;
   yy1=y1+i*(h+5);
   yy2=y1+(i+1)*(h+5);

   area=imagesize(x1,yy1,x2,yy2);
   buffer[i]=malloc(area);
   getimage(x1,yy1,x2,yy2,buffer[i]);

 }
}

getresponse(char **menu,char **buffer,int width,int count,int x1,int 
y1)
{
 int choice=1,prevchoice=0,x,y,x2,y2,button;
 int in,i,h;

 h=textheight(menu[0]);
 y2=y1+count*(h+5);
 x2=x1+width;
 rectangle(x1-5,y1-5,x2+5,y2+5);

 while(1)
 {
	 getmousepos(&button,&x,&y);

   if (x>=x1&&x<=x2&&y>=y1&&y<=y2)
  {	in=1;

	  for(i=1;i<=count;i++)
	 {
	  if(y<=y1+i*(h+5))
	 {  choice=i;
	break;
	  }
	  }

	  if(prevchoice!=choice)
	 {
	hidemouseptr();
	highlight(buffer,choice,h,x1,y1);

	 if(prevchoice)
	   dehighlight(buffer,prevchoice,h,x1,y1);

	 prevchoice=choice;
	 showmouseptr();
	  }
	if((button & 1)==1)
	  {
	while ((button & 1)==1)
	  getmousepos(&button,&x,&y);

	if(x>=x1&&x<=x2&&y>=y1&&y<=y2)
	return(choice);
	   }
	}
  else
  {
   if( in ==1)
   {
	in=0;
	prevchoice=0;
	hidemouseptr();
	dehighlight(buffer,choice,h,x1,y1);
	showmouseptr();
   }
  }
 }
}

highlight(char **buffer,int ch,int h,int x1,int y1)
{
  putimage(x1,y1+(ch-1)*(h+5),buffer[ch-1],NOT_PUT);
}


dehighlight(char **buffer,int ch,int h,int x1,int y1)
{
  putimage(x1,y1+(ch-1)*(h+5),buffer[ch-1],COPY_PUT);
}


/* initmouse */
initmouse()
{
 i.x.ax=0;
 int86 (0x33,&i,&o);
 return(o.x.ax);
}

/* displays mouse pointer */
showmouseptr()
{
 i.x.ax=1;
 int86(0x33,&i,&o);
}

hidemouseptr()
{
 i.x.ax=2;
 int86(0x33,&i,&o);
}

/*gets mouse coordinates and button status*/

getmousepos(int *button,int *x,int *y)
{
 i.x.ax=3;
 int86(0x33,&i,&o);
 *button=o.x.bx;
 *x=o.x.cx;
 *y=o.x.dx;
}

movemouseptr(int *x,int *y)
{
 i.x.ax=4;
 int86(0x33,&i,&o);
 o.x.cx=*x;
 o.x.dx=*y;
 return 0;
}
4
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Aia
0

You must be joking, you are not mentioning what errors you are getting , what sort of an application this is (console or gui), what you have tried to solve those errors, ... in fact you have not even mentioned which compiler you are using !!!
And you expect us to solve those errors for you ????

0

I have seen this code before. It was written by Kanetkar in a book dedicated for the turbo c complier.


In my first glance I found these.
1) some of the codes are missing. ( initgrraph(), etc which will make a interrupt )
2) malloc returns a void pointer which needs to be type-casted.
3) stdlib.h, conio.h required.
4) no arguments for exit().
and more.
Besides, Why should you used such old compilers & its nonstandard libraries?

1

2) malloc returns a void pointer which needs to be type-casted.

In C programming, casting malloc it is asking for problems. Include the header file stdlib.h to avoid compiler warnings. Read more about it here.

Votes + Comments
thanks for the correction
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.