[英]Structure C: Storing values in an array
Can anyone advise why values are not getting stored in struct array? 谁能建议为什么值不存储在结构数组中? I tried to store values in buffer array and I notice values that are getting stored 0 or 1 not user input. 我试图将值存储在缓冲区数组中,但我注意到要存储的值不是用户输入的0或1。
This is what I tried: 这是我尝试的:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int menu(void);
struct item
{
int i_SKU;
int i_QUANTITY;
int i_PRICE;
};
int main()
{
int i,j = 0;;
int n;
int input;
//struct item item1[10] = { {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0} };
struct item item1[]={0};
struct item buff[]={0};
//printf("size of %d", sizeof(item1)/sizeof(item1[0]));
printf("Welcome to the Inventory\n");
printf("===================\n");
B: printf("Please select from the following:\n");
A: menu();
scanf("%d", &input);
switch (input)
{
case 1:
printf("Inventory\n");
printf("=========================================\n");
printf("ku Price Quant\n");
for (i = 0; i < sizeof(buff)/sizeof(buff[0]); i++)
{
printf("%d %d %d\n", buff[i].i_SKU, buff[i].i_PRICE, buff[i].i_QUANTITY);
}
printf("=========================================\n");
goto B;
case 2:
//n = sizeof(item1)/sizeof(item1[0]) + 1;
//for (i=n; i < ; i++)
printf("Please input a KU number:");
buff[j].i_SKU=scanf("%d", &item1[j].i_SKU);
printf("Quantity:");
buff[j].i_QUANTITY=scanf("%d", &item1[j].i_QUANTITY);
printf("Price:");
buff[j].i_PRICE=scanf("%d", &item1[j].i_PRICE);
printf("The item added.\n");
j=j+1;
goto B;
case 0:
printf("bye!");
exit(1);
default:
printf("Invalid input, try again:\n");
goto A;
}
return 0;
}
int menu(void)
{
printf("1) Display.\n");
printf("2) Add to inventory.\n");
printf("0) Leave.\n");
printf("Select:");
return 0;
}
I tried to store values in buffer array and I notice values that are getting stored 0 or 1 not user input. 我试图将值存储在缓冲区数组中,但我注意到要存储的值不是用户输入的0或1。
scanf
doesn't return the value it reads, it returns the number of characters it reads. scanf
不返回其读取的值,而是返回其读取的字符数。
So these lines: 所以这些行:
buff[j].i_SKU=scanf("%d", &item1[j].i_SKU);
buff[j].i_SKU=scanf("%d", &item1[j].i_QUANTITY);
buff[j].i_SKU=scanf("%d", &item1[j].i_PRICE);
don't do what you want. 不要做你想做的。 They put the integer into item1[j].i_X
, but assign buff[j].i_X
to the number of characters read. 他们将整数放入item1[j].i_X
,但将buff[j].i_X
分配给读取的字符数。
Also, I'm guessing you intended the left hand side of those three equations to differ, not all refer to i_SKU
. 另外,我猜您打算使这三个方程的左侧不同,而不是全部都参考i_SKU
。
Another possible problem: your buffers don't have a length specified, so they will probably just have storage for one element. 另一个可能的问题:您的缓冲区没有指定长度,因此它们可能仅存储一个元素。
EDIT : 编辑 :
I'm not sure what purpose the items1
array serves in your code, but if you want the value in both arrays, you can try the following: 我不确定items1
数组在代码中的用途是什么,但是如果您想在两个数组中使用该值,则可以尝试以下操作:
int sku;
printf("Please input a KU number:");
buff[j].i_SKU=scanf("%d", &sku);
item1[j].i_SKU = sku;
buff[j].i_SKU = sku;
// etc.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.