I have a few arrays that I wanted to update. The problem is that when data is added to the array, some variables are updated to 0.
my code to add data:
void addStockItem(){
system(CLS_NAME);
printf("New Item\n\n");
printf("New Item Name : ");
fflush(stdin);
scanf(" %[^\n]s",&*itemName[totalItem]);
//itemName[totalItem][30] = strupr(itemName[totalItem]);
fflush(stdin);
printf("New Item Price : ");
scanf("%f",&item_Price_Profit[totalItem][0]);
printf("New Item Profit : ");
scanf("%f",&item_Price_Profit[i][1]);
printf("New Item Qty : ");
scanf("%d",&itemQuantity[totalItem][0]);
itemQuantity[totalItem][1]=0;
itemQuantity[totalItem][2]=0;
++totalItem;
allStocks();
}
the data,
int totalItem=13,itemQuantity[][3]={8,0,0,9,0,0,11,0,0,0,0,0,20,0,0,22,0,\
0,16,0,0,18,0,0,9,0,0,7,0,0,5,0,0,12,0,0,0,0,0},sessionQuantity;
float item_Price_Profit[][2]={1,0.5,2,0.2,3,0.2,4,0.2,5,0.5,6,0.8,7,0.5,8,0.2,9,\
0.2,10,0.2,11,0.5,12,0.8,13,0.9};
char itemName[][30]={"STABILO PENCIL 2B","STABILO PEN 0.5",\
"STABILO ERASER","STABILO RULER","STABILO TEST PAD","STABILO BOOK","STABILO SCISSORS","STABILO SHARPENER","STABILO GLUE","STABILO CHALK","STABILO MARKER PEN","OXFORD DICTIONARY","STABILO HIGHLIGHTER"};
full code: http://pastebin.com/jjuCCrjz
[EDIT]
Everything work as they intended to after I changed itemQuantity[][3] to itemQuantity[100][3], item_Price_Profit[][2] to item_Price_Profit[100][2] and itemName[][30] to itemName[100][30]. What could possibly my mistake other than scanf?
I can’t access pastebin from my work computer, so I’ll have to go by what’s posted.
Several issues off the bat:
fflushis only defined to work on output streams, not input streams; the behavior offflush(stdin)is undefined (it will do something, but probably not what you want). If you need to clear out garbage from the input stream, you’ll need to consume it usinggetchar()orfgets()or similar until you see a newline or some other indicator that you’ve cleared out the garbage. The%fand%dconversion specifiers will skip over any leading whitespace, and by having the blank before the%[conversion specifier will also cause any leading whitespace to be skipped. So ditch thefflushcalls altogether.scanf(" %[^\n]s",&*itemName[totalItem]);– this looks confused. Unless you expect the input to always have a trailingscharacter, the conversion specifier should simply be%[^\n]. The&*in front ofitemNameis redundant; you just need to writescanf(" %[^\n]", itemName[totalItem]);Although you should probably put a field width specifier in there:
scanf(" %30[^\n]", itemName[titalItem]);to avoid buffer overflow.
You’re accessing all your data items (
totalItem,itemName,item_Price_Profit, etc.) as global variables. This is usually a recipe for heartburn. Ideally, functions and their callers should not share state via globals; rather, they should communicate through parameters, return values, and exceptions (where supported). Something more likevoid addStockItem(char *name, float *price, float *profit, float *quantity) { ... scanf(" %30[^\n]", name); ... scanf("%f", price); ... scanf("%f", profit); ... scanf("%d", quantity); }which would be called like
addStockItem(itemName[totalItem], &item_Price_Profit[totalItem][0], &item_Price_Profit[i][1], &itemQuantity[totalItem][0]);Your data structures feel really hinky to me, but that’s likely because I can’t see your entire program.