c - 如何在 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);
}
}
解决方案
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++;
}
这应该可以正常工作:)
推荐阅读
- model-view-controller - 如何将 data-plugin-datepicker 标签添加到 MVC @Html.EditorFor
- swift - loadImageFromDisk - 函数修改 swift
- typescript - 为什么在正确的接口传递后函数未定义?
- html - 如何在一张卡片中对齐多个参数
- design-patterns - 如果发生任何故障,Saga Pattern 是否能够帮助逆转支出?
- android - 我们可以在同一个 Android Studio 项目中使用多个数据库吗?
- google-apps-script - 表格查找条件突出显示颜色
- mysql - MySQL:在写的情况下如何避免表锁?
- google-kubernetes-engine - GKE 和 prometheus 监控
- java - 我似乎无法弄清楚我的 GUI 的问题,X 和 Y 坐标有点偏离,我的高度和宽度输入也关闭了