如何使用C程序中的功能在现有结构中添加新值?

问题描述 投票:1回答:2

我需要在现有笔划中添加一个已经存在数据的新行,我需要使用用户定义的函数添加一个新值,但是在执行add函数之后,我显示了被卡住但未显示出附加值仅显示以前的值,而不显示添加的新值。请帮助我做到这一点:)

#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);
void add(struct customer[]);

主要

int main()
{
    int a;
    char m[100];
    struct customer c1[30];
    do
    {
        printf("\n1)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);
            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("%29s", c1[i].name);
        printf("\nEnterNationality");
        scanf("%29s", c1[i].nationality);
        printf("\nEnteremailid");
        scanf("%29s", 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, // extra & in c1[i].name
               c1[i].checkintime, c1[i].checkouttime, c1[i].noofroomsreq, c1[i].noofoccupants);
    }
}

这是我需要完成的功能,以添加新值,就像追加一样

void add(struct customer c1[30])
{
    for (i = n; i < n + 1; i++)
    {
        printf("\nEnter Name of customers");
        scanf("%29s", c1[i].name);
        printf("\nEnter Nationality");
        scanf("%29s", c1[i].nationality);
        printf("\nEnter emailid");
        scanf("%29s", 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);
    }
}
c struct append
2个回答
0
投票
void add(struct customer c1[30]) { printf("\nEnter Name of customers"); scanf("%29s", c1[n].name); printf("\nEnter Nationality"); scanf("%29s", c1[n].nationality); printf("\nEnter emailid"); scanf("%29s", c1[n].email); printf("\nEnter mobileno"); scanf("%d", &c1[n].mobileno); printf("\nPeriod of stay"); scanf("%d", &c1[n].periodofstay); printf("\ncheckintime"); scanf("%d", &c1[n].checkintime); printf("\ncheckouttime"); scanf("%d", &c1[n].checkouttime); printf("\nroomsrequired"); scanf("%d", &c1[n].noofroomsreq); printf("\nNoofoccupants"); scanf("%d", &c1[n].noofoccupants); n++; }
这应该可以正常工作:)

0
投票
您忘记每次添加都在for循环之外增加n:

void add(struct customer c1[30]) { for (i = n; i < n + 1; i++) { ... } n++; }

© www.soinside.com 2019 - 2024. All rights reserved.