Вы находитесь на странице: 1из 5

Mark Anthony Legaspi December 10, 2018

BSIT – 1A

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int GetValidContactOrNot(char contact[]);
struct ContactName {
char name[20];
};

main()
{
struct ContactName ContName;
int choice,i,flag,j=1;
char contact[11],storeContact[100];

do{
if(choice==1)
{
printf("Enter Name\n");
fflush(stdin);
gets(ContName.name);
printf("Enter Mobile Number\n");
fflush(stdin);
gets(contact);
flag=GetValidContactOrNot(contact);
if(flag==1)
{
FILE *contactFile;
contactFile=fopen("Marky.txt","a");
char c;
if(contactFile==NULL)
{
printf("Contact File Not Found\n");
exit(0);
}
else
{
strcpy(storeContact,"Name : ");
strcat(storeContact,ContName.name);
strcat(storeContact,"\n");
strcat(storeContact,"Contact : ");
strcat(storeContact,contact);
strcat(storeContact,"\n");
fputs(storeContact,contactFile);
fputs("----------------------------------------------------\n",contactFile);
printf("Contact has been added Successfully\n");
}
fclose(contactFile);
}
else
{
printf("Invalid Contact Number.It should contain only numbers and should have 10
digits\n");
}
}
if(choice==2)
{
FILE *contactFile;
contactFile=fopen("Marky.txt","r");
char c;
while(1)
{
if(contactFile==NULL)
{
printf("Contact File Not Found\n");
}
else
{
c=fgetc(contactFile);
if(c==EOF)
break;
printf("%c",c);
}
}
fclose(contactFile);
}
printf("Press\n1 - Add Contact\n2 - View Contact List\nany other number to exit\n");
scanf("%d",&choice);

}while(choice==1 || choice==2);
}

int GetValidContactOrNot(char contact[])


{
int i,flag;
if(strlen(contact)==11)
{
flag=1;
for(i=0;i<strlen(contact);i++)
{
if(!isdigit(contact[i]))
flag=0;
}
}
else
{
return(0);
}
return(flag);
}
Algorithm:
Step 1: Start
Step 2: Print “ 1 – Add Contact
2 – View Contact List
Any other number to exit
Step 3: If choice == 1 to Add Contact
Print “Enter Name: “
Print “Enter Mobile Number: “
Step 4: If Mobile Number == 11 digits
Print “Contact has been added successfully”
Else
Print “Invalid contact number. It should contain only numbers and should have 11 digits”
Step 5: If choice == 2 to View Contact List
Display the contact list
Else choice is equal to any other numbers to exit
Exit the program
Step 6: End

Pseudocode:
Step 1: Start
Step 2: Declare variables name, contact, choice, I, flag, j=1, and contact
Step 3: Do
If choice ==1
Print “Enter Name: “
Print “Enter Mobile Number: “
Step 4: flag = GetValidContactOrNot (contact)
Step 5: If flag ==1
Open contact file
Step 6: If contact file == NULL
Print “Contact file not found””
Else
Print “Name” and store the name in the text file
Print “Contact” and store the contact in the text file
Step 7: Print “Contact has been added successfully”
Step 8: Close the contact file
Step 9: Else
Print “Invalid contact number. It should contain only numbers and should have 11 digits”
Step 10: If choice == 2
Open contact file
Step 11: While
If contact file == NULL
Print “Contact file not found
Else
Get the contact file
Step 12: If c == EOF
break;
print c
Close the contact file

Flowchart:
Step 13: Print “ 1 - Add Contact
2 – View Contact List
Any other number to exit
Stop 14: End

Вам также может понравиться