Hello. Wrote the following code to store phone book details.
CODE
/* Phone book */
#include<stdio.h>
#include<conio.h>
/* structure declaration */
struct
{
char name[50],city[50];
struct
{
char mobn[50],net[50];
}mob;
struct
{
char lln[50],ser[50];
int code;
}ll;
}per[10];
/* main function starts */
void main()
{
/* variable declaration */
int i,r1,r,n;
char c='y';
printf("\n How many entries do you wish to store? ");
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
printf("\n Enter name: ");
fgets(per[i].name,50,stdin);
printf("\n Enter city: ");
fgets(per[i].city,50,stdin);
printf("\n Enter mobile number: ");
fgets(per[i].mob.mobn,50,stdin);
printf("\n Enter network provider: ");
fgets(per[i].mob.net,50,stdin);
printf("\n Enter city code: ");
scanf("%d",&per[i].ll.code);
getchar();
printf("\n Enter landline number: ");
fgets(per[i].ll.lln,50,stdin);
printf("\n Enter service provider: ");
fgets(per[i].ll.ser,50,stdin);
}
while(tolower(c)=='y')
{
printf("\n Which record do you wish to review? ");
scanf("%d",r1);
r=r1-1;
printf("\n Name: %s",per[r].name);
printf("\n City: %s",per[r].city);
printf("\n Mobile Number: %s",per[r].mob.mobn);
printf("\n Network provider: %s",per[r].mob.net);
printf("\n Landline Number: %d %s",per[r].ll.code,per[r].ll.lln);
printf("\n Service provider: %s",per[r].ll.ser);
printf("\n Do you wish to continue?(Y/N) ");
getchar();
c=getchar();
}
printf("\n Have a nice day. Thank you");
getch();
}
The problem is with the display of required information in the end. Instead of the correct information, it shows some unknown symbols against 'Name: ', 'City: ' etc. Can someone please explai why this happens and how to rectify it. Thank You.