正如标题中提到的,我不知道如何在函数内将 char 数组传递给 struct 数组。
任务是编写一个小型仓库系统,您可以在其中添加/删除/搜索项目。但是为了添加/删除项目,应该检查该项目是否已经存在。
我必须使用一个结构体和该结构体的数组。我目前的方法确实有问题,我尝试了几个小时来解决这个问题,但此时我什至不知道要解决什么..
重要说明:我不允许使用除 stdio.h 和 string.h 之外的任何其他库,并且我不允许 使用 scanf 以外的任何东西进行输入和 printf 进行输出。我必须将两个字符串与 strcmp 进行比较,并且 我不认为允许我使用 strncpy。
也许我的整个方法都是错误的..
这是我的代码
#include <stdio.h>
#include <string.h>
#define LENGTHITEM 100
#define NUMBEROFITEMS 100
typedef struct WareHouse_ {
char item[LENGTHITEM];
int number;
} WareHouse;
void newItem(char*, WareHouse*, int);
int main() {
int end = 0; int inputNumber = 0;
char decision; char inputItem[NUMBEROFITEMS];
WareHouse wHouse[NUMBEROFITEMS];
printf("\nWelcome!");
printf("\n\nYou can choose the following:");
while (end != 1) {
printf("\n\nNew Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): ");
scanf("%c", &decision);
switch(decision) {
case 'h':
case 'H': printf("\nName: ");
scanf("%s, inputItem);
printf("Number: ");
scanf("%i", &inputNumber); <-- Here it says: scanf used to convert a string to an integer value (CLion)
newItem(inputItem, wHouse, inputNumber);
break;
case 'e':
case 'E': printf("\nTest E");
break;
case 's':
case 'S': printf("\nTest S");
break;
case 'a':
case 'A': printf("\nTest A");
break;
case 'b':
case 'B': printf("\nGood bye!");
end++;
break;
}
}
return 0;
}
void newItem(char *inputItem, WareHouse *wHouse , int inputNumber) {
for (int i = 0; i < NUMBEROFITEMS; i++) {
if (strcmp(inputItem, wHouse[i].item) == 0) {
printf("\nItem already exists, number increased %i", inputNumber);
wHouse[i].number+= inputNumber;
} else if (strcmp(inputItem, wHouse[i].item) < 0 || strcmp(inputItem, wHouse[i].item) > 0) {
wHouse[i].number+= inputNumber;
<-- Here I want to pass the "inputItem" into the "wHouse" array but I don't know how.. -->
}
}
}
Program:
Welcome
You can choose the following:
New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): H <-- My input
Name and number (Name Number): fish 10 <-- My input
New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): <-- BUG IT APPEARS TWO TIMES
New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): H
Name and number (Name Number): fish 10 <-- My input
<-- Here it should output: "Item already exists, number increased 10" but it does nothing -->
New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): <-- BUG IT APPEARS TWO TIMES
New Item (H) / Delete Item (E) | Search Item (S) | Table (A) | End (B): B
Good bye
最佳答案
要将字符串放入另一个数组而不使用 strncpy()
,您可以使用 strcpy()
。
strcpy(wHouse[i].item, inputItem);
另一种方法是通过 strlen()
计算要复制的长度,并通过 memcpy()
复制。
size_t len = strlen(inputItem);
memcpy(wHouse[i].item, inputItem, len + 1); /* +1 for terminating null character */
如果这两种方式都不允许,您可以自己复制每个字符。
for (size_t p = 0; ; p++) {
wHouse[i].item[p] = inputItem[p];
/* break here (not via loop condition) instead of loop condition for copying terminating null character */
if (inputItem[p] == '\0') break;
}
我在您的代码中发现了另外三个问题。
首先,数组wHouse
在没有初始化的情况下使用,
因此您使用不确定的值进行计算并调用未定义的行为。
初始化可以这样完成:
WareHouse wHouse[NUMBEROFITEMS] = {{"", 0}};
您只能指定第一个元素的值, 左边的元素会自动初始化为“0”。
其次,该选项会显示两次,因为换行符是通过 %c
读取的。
为了避免这种情况,您可以在 %c
之前添加一个空格字符,告诉 scanf
忽略空白字符。
scanf(" %c", &decision);
第三,如果您像您所说的那样在 newItem()
函数中添加“传递”,
所有元素都将被修改,因为strcmp()
返回的所有值都会修改元素。
相反,我猜您想在找不到指定项时修改第一个空元素。
要实现这一点,你可以这样写:
void newItem(char *inputItem, WareHouse *wHouse , int inputNumber) {
int itemFound = 0; /* set to 1 when specified item is found */
int firstEmptyItem = -1; /* set to the index of the first empty item */
for (int i = 0; i < NUMBEROFITEMS; i++) {
if (strcmp(inputItem, wHouse[i].item) == 0) {
printf("\nItem already exists, number increased %i", inputNumber);
wHouse[i].number+= inputNumber;
itemFound = 1; /* the item is found! */
} else if (strcmp("", wHouse[i].item) == 0) { /* check if this item is empty */
/* if empty and index is not written yet, write the index */
if (firstEmptyItem < 0) firstEmptyItem = i;
}
}
/* if the item is not found, and a room to add new item exists */
if (!itemFound && firstEmptyItem >= 0) {
int i = firstEmptyItem; /* set index (or you can use firstEmptyItem directly in the following code) */
wHouse[i].number+= inputNumber;
/* Here you should pass the "inputItem" into the "wHouse" array. */
}
}
关于c - 将 char 数组传递给函数内的结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59225527/