Code:
#include<iostream>
#include<string.h>
#include<iomanip>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
using namespace std;
void menu();
void addPatient();
void viewRecords();
void viewAllRecords();
void viewOneRecord();
void display();
void deleteMechanism();
void editRecord();
void menuDisplay();
void viewOneMechanism();
void deleteRecord();
void question();
struct studinfo
{
int idno;
char name[25];
char address[25];
};
FILE *Patfile;
FILE *tempPatfile;
int id;
bool found;
main()
{
int a;
printf("\n\n\n\n\n\n\n\t\t");
for(a=1; a<=49; a++){
printf("*");
}
printf("\n\n\n\t\t\t\t WELCOME\n\n\n\t\t");
for(a=1; a<=49; a++){
printf("*");
}
getch();
system("cls");
menu();
}
void menu()
{
int choice=0;
while(choice!='E')
{
system("cls");
menuDisplay();
printf("\nType the letter of your choice: ");
scanf("%c", &choice);
choice = toupper(choice);
switch(choice)
{
case 'A':{addPatient();break;}
case 'B':{viewRecords();break;}
case 'C':{deleteRecord();break;}
case 'D':{editRecord();break;}
}
}
}
void menuDisplay()
{
printf("\n\t\t\t P O E B E and J A I' S");
printf("\n\n\n\t\t\t\t Phonebook");
printf("\n\n\n************************************");
printf("********************************************");
printf("\n\nC H O I C E S");
printf("\n- - - - - - - - - - - - - - -");
printf("\n[A] ADD Contact");
printf("\n[B] VIEW Contacts");
printf("\n[C] DELETE ONE Contact");
printf("\n[D] EDIT Contacts ");
printf("\n[E] EXIT");
printf("\n- - - - - - - - - - - - - - -");
}
void addPatient()
{
int cntct,num;
found=false;
char name[25],address[25];
studinfo students;
printf("\n\nContact number:");
scanf("%d", &num);
Patfile=fopen("Profile.txt","a+");
rewind(Patfile);
while(!feof(Patfile)&& found==false)
{
fscanf(Patfile,"%d%s%s",&cntct,&name,&address);
if(cntct==num)
{
printf("Number is Existing!..");
found=true;
}
}
if(found==false)
{
printf("\nName:");
scanf("%*c%s", &name);
printf("\nAddress:");
scanf("%s", &address);
fprintf (Patfile, "\n%-5d %-20s %-20s",num,name,address);
printf("\nThe new record was successfully saved!");
}
getch();
fclose(Patfile);
}
void viewRecords()
{
system("cls");
char c;
printf("\nView One Contacts or View All Contacts?");
printf("\n[Press [A] to view One Record]");
printf("\n[Press [B] to view All Records]");
printf("\nEnter Choice: ");
scanf("%c", &c);
c=toupper(c);
switch(c)
{
case 'A':
viewOneRecord();
break;
case 'B':
viewAllRecords();
break;
default:
system("cls");
return viewRecords();
break;
}
question();
}
void question()
{
char e;
printf("\n\nDo you want another Transaction?[Y/N]:");
scanf("%*c%c", &e);
e=toupper(e);
switch(e)
{
case 'Y':
system("cls");
return viewRecords();
break;
case 'N':
system("cls");
return menu();
break;
default:
system("cls");
return question();
break;
}
}
void viewAllRecords()
{
int idno;
char name[25],address[25];
Patfile=fopen("Profile.txt","r");
rewind(Patfile);
if(!feof(Patfile))
{
system("cls");
cout<<" \nLIST OF CONTACTS";
display();
while(!feof(Patfile))
{
if(fscanf(Patfile,"%d%s%s",&idno,&name,&address)>0)
cout<<setw(15)<<idno<<setw(25)<<name<<setw(25)<<address<<"\n";
}
}
else{
printf("No record found in the file!");}
fclose(Patfile);
getch();
}
void viewOneRecord()
{
viewOneMechanism();
getch();
}
void deleteRecord()
{
char cont;
viewOneMechanism();
if (found==true)
{
cout<<"Are you sure you want to delete this record(y/n)?:";
cin>>cont;
if(cont=='y'||cont=='Y')
{
deleteMechanism();
cout<<"\n\nThe record was successfully deleted!";
}
else
{
cout<<"\nThe record was not deleted!";
}
getch();
}
}
void editRecord()
{
char cont;
char name[25],address[25];
viewOneMechanism();
if(found==true)
{
printf("\nAre you sure you want to update this record(y/n)?:");
scanf("%c", &cont);
cont=toupper(cont);
if(cont=='Y')
{
deleteMechanism();
Patfile=fopen("Profile.txt","a+");
cout<<"\nType Name:";
cin>>name;
cout<<"\nType Address:";
cin>>address;
fprintf (Patfile, "\n%-5d %-20s %-20s",id,name,address);
fclose(Patfile);
cout<<"\nThe record was successfully updated!";
}
else
{
cout<<"\nThe record was not updated!";
}
getch();
}
}
void display()
{
cout<<"\n"<<setw(15)<<left<<"Contact no."
<<setw(25)<<left<<"Name"
<<setw(25)<<left<<"Address"<<"\n";
}
void deleteMechanism()
{
int idno;
char name[25],address[25];
Patfile=fopen("Profile.txt","r");
tempPatfile=fopen("tempProfile.txt","w+");
rewind(Patfile);
while(!feof(Patfile))
{
if(fscanf(Patfile,"%d%s%s",&idno,&name,&address)>0)
{
if(idno!=id)
{
fprintf (tempPatfile, "\n%-5d %-20s %-20s",idno,name,address);
}
}
}
fclose(Patfile);
fclose(tempPatfile);
Patfile=fopen("Profile.txt","w+");
tempPatfile=fopen("tempProfile.txt","r");
rewind(tempPatfile);
while(!feof(tempPatfile))
{
if(fscanf(tempPatfile,"%d%s%s",&idno,&name,&address)>0)
{
if(idno!=id)
{
fprintf (Patfile, "\n%-5d %-20s %-20s",idno,name,address);
}
}
}
fclose(Patfile);
fclose(tempPatfile);
}
void viewOneMechanism()
{
int idno;
char name[25],address[25];
found=false;
printf("\n\nEnter CONTACT to be viewed:");
cin>>id;
Patfile=fopen("Profile.txt","r");
rewind(Patfile);
while(!feof(Patfile)&&found==false)
{
if(fscanf(Patfile,"%d%s%s",&idno,&name,&address)>0)
{
if(idno==id)
{
display();
cout<<setw(15)<<idno<<setw(25)<<name<<setw(25)<<address<<"\n";
found=true;
}
}
}
if(found==false)
{
printf("\n Record is not existing!");
}
fclose(Patfile);
}