As mentioned in the title, I don't know how to pass a char array to a struct array within a function.
The task is to write a little warehouse system, where you can add / remove / search for items. But in order to add / remove a Item, it should be checked whether the item already exists or not.
I have to use a struct and an array of this struct. My current approach is really buggy and I tried several hours to fix this but at this point I don't even know what to fix..
Important note: I am not allowed to use any other library than stdio.h and string.h and I am not allowed to use anything else than scanf for input and printf for output. I have to compare both strings with strcmp and I don't think that I am allowed to use strncpy.
Perhaps my whole approach is wrong..
Here is my code
#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