0

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?

1
  • 1
    please don't use scanf() it is harmful, and dangerous even. Commented Sep 28, 2012 at 13:54

2 Answers 2

3

I can't access pastebin from my work computer, so I'll have to go by what's posted.

Several issues off the bat:

  1. 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.

  2. 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.

  3. 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]);

  4. Your data structures feel really hinky to me, but that's likely because I can't see your entire program.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answering and clearing things up. I'll consider removing fflush(stdin).. I followed #2, the scanf works great, but appending the data to the array's new row causes segmentation error(segmentation error occur after appending the data). For #3, the arrays are being used in many functions. So passing it around could be very confusing, at least for me. I have very little experience in programming.
@hudadiaz: "I'll consider removing fflush(stdin)" - There's nothing to consider; you are invoking undefined behavior with that call an it is wrong in all circumstances.
@Ed I've removed it anyway. Thanks.
2

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.