Thread: Need help with a few errors

  1. #1
    Registered User
    Join Date
    Aug 2013

    Need help with a few errors

    I am currently a CS student and working on an assignment that i need some help with this program that is called "RPG inventory management". The program currently has functions that do certain things for instance reading in the items, printing a table of items and calculating the sum of the value of weight for each items.

    The following errors are from the printfromfile function and the sumweight function.

    /items> make
    gcc -Wall -ansi -g -c items.c
    items.c: In function 'PrintItemTable':
    items.c:76: warning: format '%7.2d' expects type 'int', but argument 5 has type 'int *'
    items.c:76: warning: format '%13.2f' expects type 'double', but argument 6 has type 'float *'
    items.c:76: warning: format '%9.2f' expects type 'double', but argument 7 has type 'float *'
    items.c: In function 'SumItemsWeight':
    items.c:87: error: lvalue required as left operand of assignment
    make: *** [items.o] Error 1
    item.h file
    typedef struct items item_t;
    struct items
       char name[32];
       float cost, weight;
       int dam;
       item_t *next;
     * Prototypes for milestone 1
    item_t *ReadItemsFromFile(char *file);
    void KillAllItems(item_t *itb);
    void PrintItemTable(FILE *fpout, item_t *itb);
    void WriteItemsToFile(char *file, item_t *itb);
    int CountItems(item_t *itb);
    float SumItemsWeight(item_t *itb);
     * =========================
     * Functions for milestone 1
     * =========================
     * This function reads all items in file, and returns them in file-order
     * in a dynamically created list.  Each line of the file is one item, 
     * and has format:
     * '<description>' <damage> <cost> <weight>
     * RETURNS: base pointer to items in file in file order.  NULL if file does
     *          not exist, or has no validly formated lines.
       item_t *ReadItemsFromFile(char *file)
     * As a hint, here is a line that would print success or failure of a line
     * in the proper format.  Delete this crap code but you can use the format for
     * your own read in from file.  You will simply read in lines until one fails,
     * at which point you return the base ptr to all allocated structures.
          item_t *itb;
          item_t *curr;
          item_t *prevs;
          FILE *fpin;
          fpin = fopen(file, "r");
          prevs = itb = curr = malloc(sizeof(item_t));
             curr->next = malloc(sizeof(item_t));
             fscanf(fpin,"%s %d %f %f", curr->name, &curr->dam, &curr->cost, &curr->weight);
             #if 0
                if(fscanf(fpin, " '%[^']' %d %f %f", name, &dam, &cost, &weight) == 4)
                   printf("Read the line!");
                         printf("Failed to read line!");
     * This function prints out all items in an easy-to-read table, with an item
     * on each line, each column separated by 2 spaces, and:
     *    4 digits for item number
     *    31 characters for name
     *    7 digits for damage
     *    13 characters for cost, with 2 decimal places printed
     *    9 characters for weight, with 2 decimal places printes
    void PrintItemTable
       FILE *fpout,    /* FILE stream to print to (set to stdout for screen) */
       item_t *itb     /* list of items to dump to screen */
       int i, num;
       int count = 4;
       fpout = fopen("stdout", "w");
       /* declarations here! */
       " NUM  NAME                             DAMAGE          COST    WEIGHT\n");
       "====  ===============================  ======  ============  ========\n");
       /* print all the lines */
       for(i=0; i >= count; i++)
          fprintf(fpout, "%4d  %31s  %7.2d  %13.2f  %9.2f", num, itb->name, &itb->dam, &itb->cost, &itb->weight);
     * RETURNS: cumulative weight of all items in list itb
    float SumItemsWeight(item_t *itb)
       float sum, weight;
       &itb->weight = weight;
       int i, count=4;
       for(i=0; i >= count; ++i);
          sum = sum + weight;

    currently my main concerns are the ReadItemsFromFile function and the following errors. The following errors are errors that i tired fixing and cant seem to make work and the ReadItemsFromFile function im not to sure if it has been program correctly. i am also currently working to better organize the code to make it easier to read will update if needed.

    if any changes are to be made or parts need to be worked plz explain on the reason so that i can get a better understanding of it.
    Last edited by alberto_1991; 08-04-2013 at 10:35 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    OK, so you have a pointer to a structure, right, any structure. You have to dereference it like so
    That allows you to access bar, whatever it is.

    The function printf doesn't need you to pass in a memory location to work like scanf does. You probably got the two mixed up. The ampersand is the address-of operator, so you are passing in all of the variable's addresses, when the values actually match the conversion specifier.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    to control loop on lines 29-40 you need to check the return value of fscanf.

    If it is not 4 - some fields failed to be read.

    In this case you can exit the loop.

    Additionally at this stage you can check the feof flag to see if whole file was read and ferror flag to see if some other error occurred during reading.

    To be able to recover after errors (skip some mis-formatted line for example) - you need slightly change your reading approach - and replace the fscanf with pair call to fgets+sscanf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me i have 3 errors ,
    By Melody in forum C++ Programming
    Replies: 8
    Last Post: 12-01-2007, 01:11 AM
  2. y am i getting these errors
    By vodka9er in forum C++ Programming
    Replies: 15
    Last Post: 11-19-2007, 02:30 AM
  3. 100+ errors
    By Ryu++ in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2005, 08:53 AM
  4. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM
  5. GFX Errors
    By Anglos in forum C Programming
    Replies: 1
    Last Post: 03-21-2002, 06:41 PM