In this assignment you should change the program so that it will take a number o
ID: 3642393 • Letter: I
Question
In this assignment you should change the program so that it will take a number of contacts specified by the user, rather than the hard coded 4 contacts.-----------------------------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define CONTACTS 4
//This structure is useful for recording someone's contact information.
struct Rolodex
{
char Name[50];
char TelephoneNumber[15];
};
int main()
{
int i;
char Input[100];
struct Rolodex TheRolodex[CONTACTS];
i=0;
//This loop allows us to enter the contact information from the
for(i;i<CONTACTS;i++)
{
printf("Enter Contact Number %d's Name > ",i+1);
scanf("%s",Input);
strcpy(TheRolodex[i].Name,Input);
printf(" Enter Contact Number %d's Phone Number > ",i+1);
scanf("%s",Input);
strcpy(TheRolodex[i].TelephoneNumber,Input);
}
printf(" The Contacts Entered were ");
for(i=0;i<CONTACTS;i++)
{
printf("%s ",TheRolodex[i].Name);
printf(" %s ",TheRolodex[i].TelephoneNumber);
}
return 0;
}
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//This structure is useful for recording someone's contact information.
struct Rolodex
{
char Name[50];
char TelephoneNumber[15];
};
int main()
{
int i;
char Input[100];
int contacts;
printf("Enter the number of contacts: ");
scanf("%d",&contacts);
struct Rolodex TheRolodex[contacts];
//This loop allows us to enter the contact information from the
for(i=0;i<contacts;i++)
{
printf("Enter Contact Number %d's Name > ",i+1);
scanf("%s",Input);
strcpy(TheRolodex[i].Name,Input);
printf(" Enter Contact Number %d's Phone Number > ",i+1);
scanf("%s",Input);
strcpy(TheRolodex[i].TelephoneNumber,Input);
}
printf(" The Contacts Entered were ");
for(i=0;i<contacts;i++)
{
printf("%s ",TheRolodex[i].Name);
printf(" %s ",TheRolodex[i].TelephoneNumber);
}
return 0;
}