Почему Программа останавливается сразу после опции ввода в C? - PullRequest
1 голос
/ 22 апреля 2020

Я хочу найти имя клиента из структуры, но оно завершается, не принимая никаких входных данных, если выбрать вариант поиска из меню.

Может кто-нибудь сказать мне, почему он завершается еще до того, как я вставлю какое-либо значение?

Таким образом, всякий раз, когда я go для опции 2 - search by name не принимает никакого значения, если при поиске возникнет какая-либо ошибка, я попытаюсь это исправить, но я не могу go за этим ,

#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);
    }
}

1 Ответ

1 голос
/ 22 апреля 2020

Проблема, на которую вы ссылаетесь, связана с тем, что вы передаете аргумент char array функции, которая ожидает аргумент char.

Кроме того, существуют другие проблемы, которые вызывают undefined поведение в программе, Я исправил их комментариями .

Были решены только проблемы с синтаксисом, я предполагаю, что logi c такой, какой вам нужен.

#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);
    }
}
...