【问题标题】:Why the Program stopping just after the input option in C?为什么程序在 C 中的输入选项之后停止?
【发布时间】: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


【解决方案1】:

您提到的问题与您将char array 参数传递给需要char 参数的函数有关。

除了在程序中调用undefined behavior还有其他问题,I corrected them with comments

只解决了语法问题,我认为逻辑是你需要的。

#include <stdio.h>
#include <stdlib.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);  //for a string you need a char* parameter
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("%99s", m);   //use %99s specifier to avoid container overflow, char arrays don't need &
            search(c1, m);
            break;
        case 3:
            display(c1);
            break;
        case 4:
            add(c1);
            break;
        case 5:
            exit(0); //needs stdlib.h library
            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("%29s", c1[i].name);        //strings don't need & operator, %29s specifier to avoid container overflow
        printf("\nEnterNationality");
        scanf("%29s", c1[i].nationality); //strings don't need & operator
        printf("\nEnteremailid");
        scanf("%29s", c1[i].email);       //strings don't need & operator
        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, // extra & in c1[i].name
               c1[i].checkintime, c1[i].checkouttime, c1[i].noofroomsreq, c1[i].noofoccupants);
    }
}
void search(struct customer c1[30], char* m)  //char* parameter
{

    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("%29s", c1[i].name);              //same
        printf("\nEnter Nationality");
        scanf("%29s", c1[i].nationality);       //same
        printf("\nEnter emailid");
        scanf("%29s", c1[i].email);             //same
        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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2019-02-16
    • 2012-04-07
    • 1970-01-01
    相关资源
    最近更新 更多