【发布时间】:2020-08-04 18:32:02
【问题描述】:
我想从结构中搜索客户名称,但如果选择从菜单中搜索选项,它会在不接受任何输入的情况下退出。
谁能告诉我为什么它在我插入任何值之前就退出了?
所以,每当我选择 2 - search by name 选项时,它不接受任何值,如果搜索中有任何错误,我会尝试解决这个问题,但我无法超越。
#include<stdio.h>
#include<string.h>
int n,i;
struct customer
{
char name[30];
char nationality[30];
int phoneno;
int mobileno;
char email[30];
int periodofstay;
int checkintime;
int checkouttime;
int noofroomsreq;
int noofoccupants;
};
void accept(struct customer[]);
void display(struct customer[]);
void search(struct customer[],char m);
void add(struct customer[]);
int main()
{
int a;
char m[100];
struct customer c1[30];
do
{
printf("1)FILL ARRAY\n");
printf("2)SEARCH BY NAME\n");
printf("3)PRINT ARRAY\n");
printf("4)Add Another\n");
printf("Search Operation:");
scanf("%d", &a);
switch (a)
{
case 1:accept(c1); break;
case 2: printf("Enter Name to be searched");
scanf("[^\n]",&m);
search(c1,m);
break;
case 3: display(c1); break;
case 4: add(c1); break;
case 5: exit(0); break;
default: printf("Invalid Choice");
}
} while (a != 5);
return 0;
}
void accept(struct customer c1[30])
{
printf("\n Enter no of customers");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnterNameofcustomers");
scanf("%s",&(c1[i].name));
printf("\nEnterNationality");
scanf("%s",&(c1[i].nationality));
printf("\nEnteremailid");
scanf("%s",&(c1[i].email));
printf("\nEntermobileno");
scanf("%d",&c1[i].mobileno);
printf("\nPeriodofstay");
scanf("%d",&c1[i].periodofstay);
printf("\ncheckintime");
scanf("%d",&c1[i].checkintime);
printf("\ncheckouttime");
scanf("%d",&c1[i].checkouttime);
printf("\nroomsrequired");
scanf("%d",&c1[i].noofroomsreq);
printf("\nNoofoccupants");
scanf("%d",&c1[i].noofoccupants);
}
}
void display(struct customer c1[30])
{
printf("Customer record");
printf("\nName\tNationality\temailid\tmobileno\tPeriodofstay\tcheckintime\tcheckouttime\troomsrequired\tNoofoccupants\n");
for(int i=0;i<n;i++)
{
printf("\n%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d",&c1[i].name,c1[i].nationality,c1[i].email,c1[i].mobileno,c1[i].periodofstay,
c1[i].checkintime,c1[i].checkouttime,c1[i].noofroomsreq,c1[i].noofoccupants);
}
}
void search(struct customer c1[30], char m)
{
int flag=0;
for(i=0;i<n;i++)
{
if(strcmp(c1[i].name,m)==0)
{
flag=1;
printf("\nRecord found");
printf("\n%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d",c1[i].name,c1[i].nationality,c1[i].email,c1[i].mobileno,c1[i].periodofstay,
c1[i].checkintime,c1[i].checkouttime,c1[i].noofroomsreq,c1[i].noofoccupants);
break;
}
}
if(flag==0)
printf("\n Record not found");
}
void add(struct customer c1[30])
{
for(i=n;i<n+1;i++)
{
printf("\nEnter Name of customers");
scanf("%s",&c1[i].name);
printf("\nEnter Nationality");
scanf("%s",&c1[i].nationality);
printf("\nEnter emailid");
scanf("%s",&c1[i].email);
printf("\nEnter mobileno");
scanf("%d",&c1[i].mobileno);
printf("\nPeriod of stay");
scanf("%d",&c1[i].periodofstay);
printf("\ncheckintime");
scanf("%d",&c1[i].checkintime);
printf("\ncheckouttime");
scanf("%d",&c1[i].checkouttime);
printf("\nroomsrequired");
scanf("%d",&c1[i].noofroomsreq);
printf("\nNoofoccupants");
scanf("%d",&c1[i].noofoccupants);
}
}
【问题讨论】:
-
您发布了一堵格式不正确的代码。请将其转换为格式正确的minimal reproducible example。
-
如果您的代码在文件中正确缩进,则只需将其粘贴到您的帖子中,突出显示它,然后单击类似于
{}的Code Sample按钮。如果您的代码在您的文件中没有正确缩进,那么您需要在提问之前修复它。 -
我的主要问题是我无法在案例 2 中传递搜索值,只要我选择选项 2 @EugeneSh,它就会结束。
标签: c data-structures struct