Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8604593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:36:40+00:00 2026-06-12T02:36:40+00:00

I have a few arrays that I wanted to update. The problem is that

  • 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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:36:41+00:00Added an answer on June 12, 2026 at 2:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few html image tags that fill an array. On the page
I have populated an array with a few html image tags that I have
I have a few data values that I need to store on my rails
I have a few arrays that I want to send to process with PHP.
I have two simple NSMutableArray that consists of few objects. Some of these objects
I have a few arrays and a resource that needs deletion, the value of
I have few inline sql statements with some arrays and if loops. I want
I have a few nested view models with observable arrays and am unable to
I have a few hundred words in arrays in a plist, somewhat like below:
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.