首页 > 解决方案 > 如何在 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);
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);
    }
}

标签: cstructappend

解决方案


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++;
}

这应该可以正常工作:)


推荐阅读