Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
48 views5 pages

Mark Anthony Legaspi December 10, 2018 Bsit - 1A

This program allows users to add and view contacts from a text file. It uses functions to validate phone numbers are 11 digits long and contain only numbers. When adding a contact, the user enters a name and phone number. If the number is valid, it is written to the text file along with the name. When viewing contacts, the entire text file is displayed. The program loops until the user chooses to exit. It uses structures to store the name and validation functions to check the phone number format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views5 pages

Mark Anthony Legaspi December 10, 2018 Bsit - 1A

This program allows users to add and view contacts from a text file. It uses functions to validate phone numbers are 11 digits long and contain only numbers. When adding a contact, the user enters a name and phone number. If the number is valid, it is written to the text file along with the name. When viewing contacts, the entire text file is displayed. The program loops until the user chooses to exit. It uses structures to store the name and validation functions to check the phone number format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 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

You might also like