Hello there,

My teacher in programming require to make a phone book style using C language and I almost done this project and I trying to sort up some minor problems. Would somebody our there help me? (please try to run this program by your own to see what I mean and use the password written in the code): In ADD PERSONAL PROFILE section, when you input a data there it will save in the database and no problem with that. The problem is, when I try to DELETE PERSONAL PROFILE. I can delete it but the PROFILE number still there. I want it to be deleted also when I delete the data. Please give me suggestions and also how to use backspace when the user mistaken to type his password. Still I cannot figure out this. My teacher ask me to fix that. Lastly, could you give me ideas and suggestions to make my final project more user's friendly interface?

This coming Friday is my submission I hope that I have still time to modify this project.

Thank you for your time.

Looking forward to hear from you very soon.

June

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<dos.h>
#include<stdlib.h>
#include<ctype.h>

void add(void);
void edit(void);
void erase(void);
void search(void);
void view(void);
void menu(void);
void control(void);
void fname(void);
void lname(void);

struct info
	{
	char lname[50];
	char fname[50];
	int accnt;
	char address[50];
	char contact[11];

       int buff;
      }
	phonebook[50];
FILE *fp;
void main(void)
	{
	clrscr();
	int a,k,x,y;
	char pass[9]="villarin",user[5]="alain";

	gotoxy(30,2);printf("IN PARTIAL FULLFILMENT ");
	gotoxy(40,4);printf("OF");
	gotoxy(38,6);printf("CPE 331");
	gotoxy(35,10);printf("Submitted by:");
	gotoxy(33,12);printf("Alain P. Villarin");
	gotoxy(31,13);printf("Danilo A. Bongcalas Jr.");
	gotoxy(35,16);printf("Submitted to:");
	gotoxy(27,18);printf("Engr. Maria Christina B. Condez");
	gotoxy(35,23);printf("Loading...");
	for(a=1;a<=3;a++)
	{
	if(a==1)
	{ gotoxy(32,25);printf("Checking files...");

	}
	if(a==2)
	{gotoxy(32,25);printf("Checking folders...");}
	if(a==3)
	{gotoxy(32,25);printf("Checking system...");}
	for(k=1;k<=79;k++)
	{
	gotoxy(k,24);printf("||");
	}
	for(k=1;k<=79;k++)
	{
	gotoxy(k,24);putch(219);
	delay(50);

	}
	}



     do
     {          pass:
	     clrscr();
	 gotoxy(30,4);printf("PERSONAL PROFILE");
	 gotoxy(25,10);printf("Enter user's name: ");
	 gets(user);
	 gotoxy(25,11);printf("Enter user's password: ");

	for(x=0;x<8;x++)
     {
	pass[x]=getch();
	printf("*",pass[x]);
     }
	 if(strcmp(user,"alain")!=0||strcmp(pass,"villarin")!=0)
	 {
	 gotoxy(22,20);printf("The Username And Password did not match!!!");delay(1000);
	goto pass;
	getch();

	 }
 }

    while(strcmp(user,"alain")!=0||strcmp(pass,"villarin")!=0);
	 gotoxy(35,23);printf("Access Granted...");

    for(a=1;a<2;a++)
   {
	for(k=1;k<=79;k++)
	{gotoxy(k,24);printf("||");}
	for(k=1;k<=79;k++)
	{gotoxy(k,24);putch(219);delay(50);}

   }

	clrscr();
	if ((fp=fopen("pb.txt","rb"))==NULL)
	     {
		if ((fp=fopen("pb.txt","wb"))==NULL)
		 {
			printf("Unable to create pb.txt data!!!");
			getch();
			exit(0);
		 }
	    phonebook[0].buff = 0;
	     fwrite(&phonebook,sizeof(phonebook),1,fp);
	     fclose(fp);
	     goto jump;
	     }
       fclose(fp);
 jump:
 menu();
	}

void menu(void)
{
char x;

select:
clrscr();
int a,k;
gotoxy(25,5); printf("[1] ADD PERSONAL PROFILE");
gotoxy(25,6); printf("[2] VIEW PERSONAL PROFILE");
gotoxy(25,7); printf("[3] SEARCH PERSONAL PROFILE");
gotoxy(25,8); printf("[4] MODIFY PERSONAL PROFILE");
gotoxy(25,9); printf("[5] DELETE PERSONAL PROFILE");
gotoxy(25,10);printf("[6] EXIT\n");
gotoxy(25,13);printf("Please Choose One : ");

x = getch();

switch(x) {

case '1' :
 {

	gotoxy(35,23);printf("Loading...");
	for(a=1;a<2;a++)
      {
	for(k=1;k<=79;k++)
	{gotoxy(k,24);printf("||");}
	for(k=1;k<=79;k++)
	{gotoxy(k,24);putch(219);delay(50);}

       }
	add();break;
 }

case '2' : {

	gotoxy(35,23);printf("Loading...");
	for(a=1;a<2;a++)
   {
	for(k=1;k<=79;k++)
	{gotoxy(k,24);printf("||");}
	for(k=1;k<=79;k++)
	{gotoxy(k,24);putch(219);delay(50);}

   }
	view(); break;
	}

case '3' : {
		//splush
	 gotoxy(35,23);printf("Loading...");
	for(a=1;a<2;a++)
   {
	for(k=1;k<=79;k++)
	{gotoxy(k,24);printf("||");}
	for(k=1;k<=79;k++)
	{gotoxy(k,24);putch(219);delay(50);}

   }

	 search();break;
	 }

case '4' : {
	       //	splush
	gotoxy(35,23);printf("Loading...");
	for(a=1;a<2;a++)
   {
	for(k=1;k<=79;k++)
	{gotoxy(k,24);printf("||");}
	for(k=1;k<=79;k++)
	{gotoxy(k,24);putch(219);delay(50);}

    }

	edit(); break;
	}

case '5' : {
		//splush
	gotoxy(35,23);printf("Loading...");
	for(a=1;a<2;a++)
    {
	for(k=1;k<=79;k++)
	{gotoxy(k,24);printf("||");}
	for(k=1;k<=79;k++)
	{gotoxy(k,24);putch(219);delay(50);}

    }

	erase();break;
	}

case '6' : {
		//splush
	gotoxy(35,23);printf("Exiting System...");
	for(a=1;a<2;a++)
    {
	for(k=1;k<=79;k++)
	{gotoxy(k,24);printf("||");}
	for(k=1;k<=79;k++)
	{gotoxy(k,24);putch(219);delay(50);}

     }
	exit(0);break;
	}

default :
	{
    gotoxy(44,13);printf("INVALID INPUT");
    delay(1000);
	goto select;
	getch();
	menu();
	}
}
}

void add(void)
{
char val[20];
clrscr();
int Tempwrite;
int x,z,len;
clrscr();

if ((fp=fopen("pb.txt","rb"))==NULL)
	{
	 printf("Cant find database!!");
	 getch();
	 exit(0);
	}
fread(&phonebook,sizeof(phonebook),1,fp);
fclose(fp);

if ((fp=fopen("pb.txt","wb"))==NULL)
	{
		printf("Database not found!!");
		getch();
		exit(0);
	}
fread(&phonebook,sizeof(phonebook),1,fp);

Tempwrite = phonebook[0].buff+1;
   gotoxy(32,4); printf("Profile Number : %d\n",Tempwrite);
   phonebook[Tempwrite].accnt = Tempwrite;
  A: gotoxy(32,5); printf("Your last name: ");
   gets(phonebook[Tempwrite].lname);
   len=strlen(phonebook[Tempwrite].lname);

    for(z=0;z<=len;z++)
    {
	if((phonebook[Tempwrite].lname[z]<=65)||(phonebook[Tempwrite].lname[z]>=122))
	{
       gotoxy(47,5);printf("                     ",phonebook[Tempwrite].lname[z]);
	goto A;
	}
	else
	goto B;
    }
	if(strcmp(phonebook[Tempwrite].lname,"")==0)
	{
	goto A;
	}

    B:   gotoxy(32,6); printf("Your first name: ");
   gets(phonebook[Tempwrite].fname);
   len=strlen(phonebook[Tempwrite].fname);

    for(z=0;z<=len;z++)
    {
	if((phonebook[Tempwrite].fname[z]<=65)||(phonebook[Tempwrite].fname[z]>=122))
	{
       gotoxy(48,6);printf("                       ",phonebook[Tempwrite].fname[z]);
	goto B;
	}
	else
	goto C;
    }
	if(strcmp(phonebook[Tempwrite].fname,"")==0)
	{
	goto B;
	}

  C: gotoxy(32,7); printf("Your Address: ");
   gets(phonebook[Tempwrite].address);
   len=strlen(phonebook[Tempwrite].address);

    for(z=0;z<=len;z++)
    {
	if((phonebook[Tempwrite].address[z]<=65)||(phonebook[Tempwrite].address[z]>=122))
	{
       gotoxy(45,7);printf("                     ",phonebook[Tempwrite].address[z]);
	goto C;
	}
	else
	goto D;
    }
	if(strcmp(phonebook[Tempwrite].address,"")==0)
	{
	goto C;
	}

  a:
  D: gotoxy(32,8); printf("Your contact number: ");
   gets(phonebook[Tempwrite].contact);
   len=strlen(phonebook[Tempwrite].contact);
    for(z=0;z<len;z++)
    {
	if(((phonebook[Tempwrite].contact[z]<=57)
	&&(phonebook[Tempwrite].contact[z]>=48))&&((len==7)||(len==11)))
	  {
	  goto b;
	  }
	 else
	 {
	 gotoxy(52,8);printf("                     ",phonebook[Tempwrite].contact[z]);
	 goto D;
	 }
	 b:
     }


  phonebook[0].buff = Tempwrite;
   fwrite(&phonebook,sizeof(phonebook),1,fp);
   fclose(fp);

menu();
}

void edit(void)
{
clrscr();
int x,y,esc,i,len;
char am[20];
char a[30];
if ((fp=fopen("pb.txt","rb"))==NULL)
	{
	 printf("Cant find database!!!");
	 getch();
	 exit(0);
	}
fread(&phonebook,sizeof(phonebook),1,fp);
	gotoxy(5,5);printf ("Profile Number : ");gets(a);
for (x =0;;x++)
	{
	if (phonebook[0].buff== 0)
	{
	gotoxy(38,10);
	printf("No data");
	break;
	}
	if (x == phonebook[0].buff+1)
	break;
	else
	{
	if (atoi(a)==phonebook[x].accnt)
	{
      gotoxy(2,7);printf("Profile #:");	gotoxy(15,7);printf ("%d",phonebook[x].accnt	);
      gotoxy(2,8);printf("Last Name:");	gotoxy(15,8);printf (phonebook[x].lname     );
       gotoxy(2,9);printf("First Name:");gotoxy(15,9);printf (phonebook[x].fname	);
       gotoxy(2,10);printf("Address:");	gotoxy(15,10);printf (phonebook[x].address  );
       gotoxy(2,11);printf("Contact #:");gotoxy(15,11);printf (phonebook[x].contact  );


	for(;;)
	{


	gotoxy(15,22);printf("Are you sure you want to replace[Y/N]...");
	esc=getch();
	if(esc==121||esc==89)
	{
	break;
	}

	else if(esc==110||esc==78)
	{
	menu();
	}

	 else

	 gotoxy(30,15);printf("INVALID INPUT!!!");
	delay(1000);



	}


	break;
	}
	}
	}
if ((fp=fopen("pb.txt","wb"))==NULL)
	{
	 printf("Cant find database!!");
	 getch();
	 exit(0);
	}

       E:  gotoxy(30,11);printf ("Enter Last Name  : ");
	gets(phonebook[x].lname);
	len = strlen(phonebook[x].lname);

       for(i=0;i<=len;i++)
	{
		if((phonebook[x].lname[i]<=65)||(phonebook[x].lname[i]>=122))
		{
		 gotoxy(49,11);printf("                 ",phonebook[x].lname[i]);
		 goto E;
		 }
		 else
		 goto F;
	  }
	       if(strcmp(phonebook[x].lname,"")==0);
	       {
		goto E;
		}

      F:  gotoxy(30,12);printf ("Enter First Name : ");
       gets(phonebook[x].fname);
       len = strlen(phonebook[x].fname);

       for(i=0;i<=len;i++)
       {
		if((phonebook[x].fname[i]<=65)||(phonebook[x].fname[i]>=122))
		{
		gotoxy(49,12);printf("                   ",phonebook[x].fname[i]);
		goto F;
		}
		else
		goto G;
	}
		if(strcmp(phonebook[x].fname,"")==0)
		{
		goto F;
		}

       G: gotoxy(30,13);printf ("Enter Address: ");
       gets(phonebook[x].address);
       len = strlen(phonebook[x].address);

       for(i=0;i<=len;i++)
       {
		if((phonebook[x].address[i]<=65)||(phonebook[x].address[i]>=122))
		{
		gotoxy(45,13);printf("                    ",phonebook[x].address[i]);
		goto G;
		}
		else
		goto H;

		if(strcmp(phonebook[x].address,"")==0);
		{
		goto G;
		}
	}


       num:
       H:   gotoxy(30,14);printf ("Enter Contact number: ");
       gets(phonebook[x].contact);
       len = strlen(phonebook[x].contact);

       for(i=0;i<len;i++)
       {
		if(((phonebook[x].contact[i]<=57)
		&&(phonebook[x].contact[i]>=48))&&((len==7)||(len==11)))
		{
		goto value;
		}
		else
		{
		gotoxy(52,14);printf("                      ",phonebook[x].contact[i]);
		goto H;
		}
		value:
	  }



   fwrite(&phonebook,sizeof(phonebook),1,fp);
   fclose(fp);

getch();
menu();
}

void view(void)
{
int x,y;
clrscr();
if ((fp=fopen("pb.txt","rb"))==NULL)
	{
	 printf("Cant find database!!");
	 getch();
	 exit(0);
	}
fread(&phonebook,sizeof(phonebook),1,fp);
	for(y=1;y<80;y++)
	{gotoxy(y,3);printf("-");}
	for(y=1;y<80;y++)
	{gotoxy(y,5);printf("-");}
	gotoxy(30,2);printf("LIST OF PERSONAL RECORD");
	gotoxy(2,4);printf("Prof.#");
	gotoxy(10,4);printf ("Last name");
	gotoxy(25,4);printf("First Name");
	gotoxy(46,4);printf ("Address");
	gotoxy(68,4);printf ("Contact #");
	gotoxy(30,25);printf("Press any key to continue...");


for (x =1;;x++)
	{
	if (phonebook[0].buff== 0)
	{
	gotoxy(37,10);printf("No data");
	break;
	}
	if (x == phonebook[0].buff+1)
	break;
	else
	{
	gotoxy(2,5+x);printf ("%d",phonebook[x].accnt	);
	gotoxy(10,5+x);printf (phonebook[x].lname     );
	gotoxy(25,5+x);printf (phonebook[x].fname     );
	gotoxy(46,5+x);printf (phonebook[x].address   );
	gotoxy(68,5+x);printf (phonebook[x].contact   );

	}
	}
getch();
menu();
}

void search(void)
{
char x;

choice:
clrscr();
gotoxy(33,5);printf("[1] Search by profile #");
gotoxy(33,6);printf("[2] Search First name");
gotoxy(33,7);printf("[3] Search by last name");
gotoxy(33,10);printf("Please choose One : ");
x = getch();
switch(x) {
case '1' : {control(); break; }
case '2' : {fname(); break;}
case '3' : {lname();break;  }
default :
	{
       gotoxy(53,10);printf("INVALID INPUT");delay(1000);
	goto choice;
	getch();
	search();
	}
}
}

void control(void)
{
clrscr();
int x;
char a[30];
if ((fp=fopen("pb.txt","rb"))==NULL)
	{
	 printf("Cant find database!!");
	 getch();
	 exit(0);
	}
fread(&phonebook,sizeof(phonebook),1,fp);
	gotoxy(5,4);printf ("Profile Number : ");gets(a);
for (x =0;;x++)
	{
	if (phonebook[0].buff== 0)
	{
	gotoxy(36,5);printf("No data");
	break;
	}
	if (x == phonebook[0].buff+1)
	break;
	else
	{
	if (atoi(a)==phonebook[x].accnt)
	{
gotoxy(5,5);printf("Last Name: ");gotoxy(21,5);printf (phonebook[x].lname    );
gotoxy(5,6);printf("First Name: ");gotoxy(21,6);printf (phonebook[x].fname	);
gotoxy(5,7);printf("Address:");gotoxy(21,7);printf (phonebook[x].address);
gotoxy(5,8);printf("Contact Number:");gotoxy(21,8);printf (phonebook[x].contact	);

	break;
	}
	}
	}
getch();
menu();

}


void fname(void)
{
clrscr();
int x;
char a[30];
if ((fp=fopen("pb.txt","rb"))==NULL)
	{
	 printf("Cant find database!!");
	 getch();
	 exit(0);
	}
fread(&phonebook,sizeof(phonebook),1,fp);
	gotoxy(5,5);printf ("Enter first name : ");gets(a);
for (x =0;;x++)
	{
	if (phonebook[0].buff== 0)
	{
	gotoxy(36,5);printf("No data");
	break;
	}
	if (x == phonebook[0].buff+1)
	break;
	else
	{
	if (strcmp(phonebook[x].fname,a)==0)
	{
gotoxy(5,6);printf("Profile number:");gotoxy(21,6);printf ("%d",phonebook[x].accnt  );
gotoxy(5,7);printf("Last Name: ");gotoxy(21,7);printf (phonebook[x].lname);
gotoxy(5,8);printf("Address:");gotoxy(21,8);printf (phonebook[x].address);
gotoxy(5,9);printf("Contact Number:");gotoxy(21,9);printf (phonebook[x].contact	);

	break;
	}
	}
	}
getch();
menu();

}


void lname(void)
{
clrscr();
int x;
char a[30];
if ((fp=fopen("pb.txt","rb"))==NULL)
	{
	 printf("Cant find database!!");
	 getch();
	 exit(0);
	}
fread(&phonebook,sizeof(phonebook),1,fp);
	gotoxy(5,4);printf ("last Name : ");gets(a);
for (x =0;;x++)
	{
	if (phonebook[0].buff== 0)
	{
	gotoxy(36,5);printf("No data");
	break;
	}
	if (x == phonebook[0].buff+1)
	break;
	else
	{
	if (strcmp(phonebook[x].lname,a)==0)
	{
gotoxy(5,5);printf("Profile number:");gotoxy(21,5);printf ("%d",phonebook[x].accnt  );
gotoxy(5,6);printf("First Name:");gotoxy(21,6);printf (phonebook[x].fname	);
gotoxy(5,7);printf("Address:");gotoxy(21,7);printf (phonebook[x].address);
gotoxy(5,8);printf("Contact Number:");gotoxy(21,8);printf (phonebook[x].contact	);

break;
	}
	}
	}
getch();
menu();
}

void erase(void)
{
clrscr();
int x,y;
char am[20];
char a[30];
int esc;
if ((fp=fopen("pb.txt","rb"))==NULL)
	{
	 printf("Cant find database!!");
	 getch();
	 exit(0);
	}
fread(&phonebook,sizeof(phonebook),1,fp);
	gotoxy(33,5);printf ("Enter Profile Number : ");gets(a);

for (x =0;;x++)
	{
	if(phonebook[0].buff== 0)
	{

	gotoxy(36,5);printf("No data");
	break;
	}
	if (x == phonebook[0].buff+1)
	break;
	else
	{
	if (atoi(a)==phonebook[x].accnt)
	{
       gotoxy(5,7);printf("Profile #:");gotoxy(20,7);printf ("%d",phonebook[x].accnt	);
       gotoxy(5,8);printf("Last Name:");gotoxy(20,8);printf (phonebook[x].lname);
       gotoxy(5,9);printf("First Name:");gotoxy(20,9);printf (phonebook[x].fname	);
       gotoxy(5,10);printf("Address:");	gotoxy(20,10);printf (phonebook[x].address);
       gotoxy(5,11);printf("Contact #:");gotoxy(20,11);printf (phonebook[x].contact	);



	for(;;)
	{


	gotoxy(15,22);printf("Are you sure you want to delete[Y/N]...");
	esc=getch();
	if(esc==121||esc==89)
	{
	break;
	}

	else if(esc==110||esc==78)
	{
	menu();
	}

	 else

	gotoxy(30,15);printf("INVALID INPUT!!!");
	delay(1000);



	}



	break;
	}
	}
	}

if ((fp=fopen("pb.txt","wb"))==NULL)
	{
	 printf("Cant find database!!");
	 getch();
	 exit(0);
	}

	phonebook[x].lname[0] = '\0';
	phonebook[x].fname[0]  = '\0';
	phonebook[x].address[0]='\0';
	phonebook[x].contact[0]  = '\0';

   fwrite(&phonebook,sizeof(phonebook),1,fp);
   fclose(fp);
getch();
menu();
}

The problem is, when I try to DELETE PERSONAL PROFILE. I can delete it but the PROFILE number still there. I want it to be deleted also when I delete the data.

You need to do one of two things to truly erase the data:

  • Overwrite the record in your file with default data (such as whitespace)
  • Rewrite your file and exclude the record you want to delete

The second option is usually best, but since rewriting the file is an intensive operation, it can make more sense (for smaller databases) to work with records in memory and write all changes en masse.

Please give me suggestions and also how to use backspace when the user mistaken to type his password. Still I cannot figure out this.

You need to recognize and properly handle the '\b' character. Note that with getch(), all of the conveniences of the shell are removed, so you have to implement even the smallest thing:

#include <stdio.h>
#include <conio.h>
 
int keybd_getc()
{
    return getch();
}
 
void keybd_putc(int ch)
{
    putch(ch);
}
 
void keybd_puts(const char *s)
{
    while (*s != '\0')
        keybd_putc(*s++);
}
 
char *get_password(char buf[], size_t limit)
{
    int done = 0;
    size_t i = 0;

    while (!done && i < limit - 1) {
        int ch = keybd_getc();
 
        switch (ch) {
        case '\r':
            keybd_putc('\n');
            done = 1;
            break;
        case '\b':
            if (i > 0) {
                --i;
                keybd_puts("\b \b");
            }
            break;
        default:
            buf[i++] = (char)ch;
            keybd_putc('*');
        }
    }
    
    buf[i] = '\0';
 
    return buf;
}
 
int main()
{
    char pass[BUFSIZ];
    
    fputs("Enter a password: ", stdout);
    fflush(stdout);

    get_password(pass, sizeof pass);
    printf("Password was '%s'\n", pass);
}

Ok thanks. I will try it later.

Hello one thing, could you give me sample code how to sort out: when I add PERSONAL PROFILE then save it and I can DELETE PROFILE including PROFILE NUMBER. I'm confuse now because of many thing in my head.

Thank you.

I'm confuse now because of many thing in my head.

Then simplify. If you're overwhelmed, throw stuff out until it becomes manageable. Once you have a bunch of manageable pieces working, putting them all together is easier.

Narue is right on the money here. The KISS principle is the foundation of good programming.

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.