Programming in C language to be run on code blocks on Windows (Structure Usage)
ID: 3701238 • Letter: P
Question
Programming in C language to be run on code blocks on Windows
(Structure Usage) Your goal in this program is to write an address book. Your progran shonld present the user with three menu options: 1. Display the current people in the address book 2. Add a new person to the address book 3. Quit the program Your adtress book should use a structure to store the first name, last name, phone number and email address of each person. You don't need to use any file V'O, just use an aray of your structure to store each entry. You can assume that the address book has a maximum capacity of 100 people that it can storeExplanation / Answer
#include<stdio.h>
#include<stdlib.h>
struct addressBook
{
char firstName[30];
char lastName[30];
int phoneNumber;
char email[30];
}arr[100];
int count = 0;
void display()
{
int i;
printf(" No of entries in Phonebook : %d ",count);
printf("---------------------------------------------------------------------------------------------------- ");
printf("First Name Last Name Phone Number Email ");
printf("---------------------------------------------------------------------------------------------------- ");
for(i=0;i<count;i++)
{
printf("%-20s %-20s %-20d %-40s ",arr[i].firstName,arr[i].lastName,arr[i].phoneNumber,arr[i].email);
}
printf("---------------------------------------------------------------------------------------------------- ");
}
void add()
{
printf("Enter first name :");
scanf("%s",&arr[count].firstName);
printf("Enter last name :");
scanf("%s",&arr[count].lastName);
printf("Enter phone number :");
scanf("%d",&arr[count].phoneNumber);
printf("Enter email id :");
scanf("%s",&arr[count].email);
count++;
}
int main()
{
int n,i,ch;
while(1)
{
printf("----------------------------------------- ");
printf(" Menu ");
printf("-----------------------------------------");
printf(" 1:Display the current people in address book");
printf(" 2:Add new person to address book");
printf(" 3:Quit the program");
printf(" ---------------------------------------- ");
printf("Enter Your Choice : ");
scanf(" %d",&ch);
switch(ch)
{
case 1: display();
break;
case 2: add();
break;
case 3: exit(0);
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.