Thread: problem with displaying doubles in array

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    problem with displaying doubles in array

    ok during this program whenever i try to display this "inventory" i get a string of numbers that doesnt match the numbers inputted...

    heres the structure for the inventory
    Code:
    typedef struct
       {
       int   day;
       int   month;
       int   year;
       } DATE_S;
    
    typedef struct 
       {
       char    bname[MAXN];  /* brand name  */
       char    bcomp[MAXN];  /* bottling company */
       int     qty;          /* quantity in stock */
       double     cost;         /* the current cost of the item */
       DATE_S  date;         /* the date of the last shipment */
       double     price;        /* the selling price of the item */
       } ITEM_S;
    heres the function for adding the inventory:
    Code:
    void  add_new_item(ITEM_S items[], int *pnitem)
    {
    ITEM_S   *pnew;
    char     *p;
    char     buf[MAXN];
    
    
    if ( *pnitem >= MAXITEMS )
       {
       printf("Out of memory [ Max items in list %d]\n", MAXITEMS);
       return;
       }
    
    pnew = &items[*pnitem];
    
    
    /* 1 */
    for ( ; ; )
       {
       printf("Enter brand name [Enter to abort] \n");
       if ( fgets(pnew->bname, MAXN, stdin) == NULL )
           return;
       if ( pnew->bname[0] == '\n' )
           return;
       if ( p = strrchr(pnew->bname, '\n') )
           *p = '\0';
       if ( find_item_in_list(items, *pnitem, pnew) )
          printf("Brand name already in list\n");
       else
          break;
       }
    
    /* 2 */
    printf("Enter bottling company\n");
    fgets(pnew->bcomp, MAXN, stdin);
    if ( p = strrchr(pnew->bcomp, '\n') )
       *p = '\0';
    
    /* 3 */
    printf("Enter quantity in stock\n");
    fgets(buf, MAXN, stdin);
    pnew->qty = atoi(buf);
    
    /* 4 */
    printf("Enter the current cost of the item\n");
    fgets(buf, MAXN, stdin);
    pnew->cost = atoi(buf);
    
    /* 5 */
    printf("Enter the selling price of the item \n");
    fgets(buf, MAXN, stdin);
    pnew->price = atoi(buf);
    
    /* 6 */
    printf("Enter the day of the last shipment \n");
    fgets(buf, MAXN, stdin);
    pnew->date.day = atoi(buf);
    
    printf("Enter the month of the last shipment \n");
    fgets(buf, MAXN, stdin);
    pnew->date.month = atoi(buf);
    
    printf("Enter the year of the last shipment \n");
    fgets(buf, MAXN, stdin);
    pnew->date.year = atoi(buf);
    
    ++*pnitem;
    
    printf("\n\n");
    }
    heres the function for displaying the code...
    Code:
    void  print_list(ITEM_S items[], int nitem)
    {
    int   i;
    
    
    if ( nitem == 0 )
       printf("The list is empty\n\n");
    else
       for ( i = 0 ; i < nitem ; ++i )
           printf("%8s %8s %4d %2d %2d %d/%d/%d\n",
               items[i].bname, items[i].bcomp, items[i].qty, items[i].cost,items[i].price,
               items[i].date.day, 
               items[i].date.month,
               items[i].date.year);    
    printf("\n");
    }
    anybody know why the numbers arent saving correctly?

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Use atof instead of atoi to convert a string to double

    Try %f instead of %d to printf a double

    (%lf to scanf a double)
    Last edited by spydoor; 04-21-2006 at 01:46 PM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    for the item[i].cost and item[i].price i still dont get doubles... ='(

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use sscanf(), with %lf. Remember printf() takes %f.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Quote Originally Posted by dwks
    Use sscanf(), with %lf. Remember printf() takes %f.
    how would that look in code.. i dont really know how to use sscanf with this function.. sry...

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, look it up on google. The first hit for "sscanf" is this: http://www.cplusplus.com/ref/cstdio/sscanf.html

    Use something like this:
    Code:
    sscanf(buffer, "%lf", &adouble);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  2. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  3. Problem with array in displaying a string
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2004, 04:45 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. help w/ array problem
    By rhythm313 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2002, 12:12 AM