Reputation: 35
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?
Upvotes: 0
Views: 154
Reputation: 123468
I can't access pastebin from my work computer, so I'll have to go by what's posted.
Several issues off the bat:
fflush
is only defined to work on output streams, not input streams; the behavior of fflush(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 using getchar()
or fgets()
or similar until you see a newline or some other indicator that you've cleared out the garbage. The %f
and %d
conversion 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 the fflush
calls altogether.
scanf(" %[^\n]s",&*itemName[totalItem]);
- this looks confused. Unless you expect the input to always have a trailing s
character, the conversion specifier should simply be %[^\n]
. The &*
in front of itemName
is redundant; you just need to write
scanf(" %[^\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 like
void 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.
Upvotes: 3
Reputation: 399863
This:
scanf(" %[^\n]s",&*itemName[totalItem]);
can't be right. This is passing a character converted to a pointer, where scanf()
expects a pointer to character. You probably mean:
scanf(" %[^\n]s", itemName[totalItem]);
Upvotes: 2