#include <stdio.h>
typedef struct pduct {char name[20];
int price;
int stock;} PRODUCT;
void init(PRODUCT * product)
{
printf("What is the name of the product: ");
fgets(product->name, 20, stdin);
printf("DEBUG: Did it get written...: %s", product->name);
printf("What is the current stock of the item: ");
scanf("%d", product->stock);
printf("What is the price of the new item: ");
scanf("%d", product->price);
}
int main()
{
PRODUCT products[5];
init(products);
return 0;
}
Now, I'm at a bit of a loss, really. Upon running this, it will ask for the name of the product, print it out so I know it stored it, then ask for the stock amount, where it will crash and return -1.
I have no idea what's going wrong. I've tried swapping out fgets with scanf, just to be sure, but the same thing happens. I'm guessing my struct is set up wrong, but I don't know how. Is it the char array maybe? Also, it's always the second input, no matter how I arrange them. So why does the first work so well?
Thanks for any help!