[英]Passing char array to a struct array within a function
正如標題中提到的,我不知道如何將 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}};
您可以只為第一個元素指定值,左邊的元素將自動初始化為“零”。
其次,選擇顯示兩次,因為換行符是通過%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. */
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.