Thread: Need help with review prog

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    Need help with review prog

    I have an final today (5PM) and I was working through the review questions. The problem is:
    A text file (A:\INV.DAT) contains 100 lines of inventory data formatted (C40, REAL, INT) for description(C40 - character length 40 bytes), unit cost(REAL), quantity on hand(INT). There is one space separating each data field. Write a program, using procedures and/or functions for items a-c below, which will do the following:

    a-10)Read the data into arrays, records, or array-records for processing. Assume 100 records.

    b-10)Compute the stock value of each item.
    Use stock value = unit cost * quantity on hand.

    c-5)Print the item description, cost, quantity, and stock value, sorted by description.

    All I need is prototypes and main (no definitions necessary) and here is what I got so far

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXRECS		100
    #define MAXC		40
    
    typedef struct Inventory Record
    {
       char  desc[MAXC];
       float unitcost;
       int   quantity
       float stockvalue;
       }InventoryRecord;
    
    void  Validate_File_Open(FILE *report, FILE *inventory); 
    void  getInventoryData(FILE * inventory, InventoryTable t, int *RecCount);
    float calcItemValue(float unitcost, int quantity);
    void  Sort_By_Description(InventoryTable t, int SIZE, int i;
    void  swap(InventoryRecord *r1, InventoryRecord *r2);
    void  printReport(InventoryTable t, int SIZE);
    void  Flush(void)
    
    typedef InventoryRecord InventoryTable[MAXRECS];
    
    Int main(void)
    {
       InventoryTable t;
       int i, count;
    
       FILE *report, *inventory;
       report = fopen("Report.txt", "w");
       inventory = fopen ("A:\\INV.DAT", "r");
       Validate_File_Open(report, inventory);
    
       getInventoryData(inventory, t, &count);
    
    	for i=0; i < MAXRECS; i++ // not sure about that
    	{  
    	    t[i].itemValue = calcItemValue(t[i].unitcost, t[i].quantity);
        }
            
        for (i = 0; i < MAXRECS; i ++)   //initialize the table
        {
           t[i] = i;
        }
        Sort_By_Description(t, i);
        PrintReport(t, i);
    
    	/*InventoryRecord tempRec=t[i];
    	T[i]=t[i+1];
    	T[i+1]=tempRec; */
    
    	/*Void swap(InventoryRecord *r1, InventoryRecord *r2);
    	swap(&t[i], &t[i+1]*/
    
        fclose(report); 
        fclose(inventory);
        Flush();
        printf("\n\n   Press any key to terminate . . . \n");
        getchar();
        return 0;
    }
    would this attempt work? I'm still confused about the struct... if you could let me know what needs fixing or fix it, I would really appreciate that.

    Thank you,
    DeathEvil
    Last edited by DeathEvil; 08-09-2007 at 02:20 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    try compiling it. You're missing some easy syntactical requirements.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    I cannot compile it because I'm missing definitions see this is just a review question for the final. I don't have time to write definitions as I'm studying few different things at the moment.
    can you throw me a bone here?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXRECS		100
    #define MAXC		40
    
    typedef struct InventoryRecord
    {
       char  desc[MAXC];
       float unitcost;
       int   quantity;
       float stockvalue;
    }InventoryRecord;
    
    typedef InventoryRecord InventoryTable[MAXRECS];
    
    void  Validate_File_Open(FILE *report, FILE *inventory); 
    void  getInventoryData(FILE *inventory, InventoryTable t, int *RecCount);
    float calcItemValue(float unitcost, int quantity);
    void  Sort_By_Description(InventoryTable t, int SIZE, int i);
    void  swap(InventoryRecord *r1, InventoryRecord *r2);
    void  printReport(InventoryTable t, int SIZE);
    void  Flush(void);
    
    int main()
    {
       InventoryTable myinven;
       InventoryTable t;
       int i, count;
    
       FILE *report, *inventory;
       report = fopen("Report.txt", "w");
       inventory = fopen ("A:\\INV.DAT", "r");
       Validate_File_Open(report, inventory);
    
       getInventoryData(inventory, t, &count);
    
    	for(i=0; i<MAXRECS; i++)
    	   /* Do you anywhere see the itemValue in the struct */      
    	   t[i].itemValue = calcItemValue(t[i].unitcost, t[i].quantity);
            
    
        for(i = 0; i < MAXRECS; i++)  
        /* You where trying to assign a value to struct data member not to the struct array itself */
           t[i] = i;
    
        /* And you forgot to send one more parameter to this function */
        Sort_By_Description(t, i , WHAT);
        PrintReport(t, i);
    
    	/*InventoryRecord tempRec=t[i];
    	T[i]=t[i+1];
    	T[i+1]=tempRec; */
    
    	/*Void swap(InventoryRecord *r1, InventoryRecord *r2);
    	swap(&t[i], &t[i+1]*/
    
        fclose(report); 
        fclose(inventory);
        Flush();
        
        printf("\n\n   Press any key to terminate . . . \n");
        getchar();
        return 0;
    }
    This code needs lot of improvement. They where lots of syntax error. If you would have compiled, you would have come to know all of them. I have managed to take off quite a few for you.

    And where are your function definitions for this program. Without that you can't use this program. And need writing them.

    ssharish2005

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you can still compile it to an object file. If you use gcc, do this:
    Code:
    gcc -c file.c
    keywords in C are always lowercase.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    thanks I will review it in a sec. Like I mentioned, there are no definitions because we won't write them (it would take way too long to write the whole program). as for gcc, my virtual machine with linux got corrupted. Any way to compile it to an object in visual 2005?

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by robwhit View Post
    you can still compile it to an object file. If you use gcc, do this:
    Code:
    gcc -c file.c
    keywords in C are always lowercase.
    It won't even compile to create an object file. Since the reference to the data members and lots of syntax error.

    Code:
    struct.c: In function &#226;main&#226;:
    struct.c:40: error: &#226;struct InventoryRecord&#226; has no member named &#226;itemValue&#226;
    struct.c:45: error: incompatible types in assignment
    struct.c:48: error: &#226;WHAT&#226; undeclared (first use in this function)
    struct.c:48: error: (Each undeclared identifier is reported only once
    struct.c:48: error: for each function it appears in.)
    ssharish2005

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by ssharish2005 View Post
    It won't even compile to create an object file. Since the reference to the data members and lots of syntax error.
    I meant so that s/he could see the errors. I know it won't compile as-is.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    OK if you meant that. I was confused with this

    you can still compile it to an object file
    ssharish2005

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    17
    ok nevermind, I fixed some of the compiler errors but right know I have too much on my plate to work on this. Thanks all for help and feel free to delete the thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly amature question: prog errors
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 10-03-2008, 01:35 PM
  2. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  3. Getting input from another prog?
    By Badman3k in forum C Programming
    Replies: 4
    Last Post: 11-11-2004, 02:58 AM
  4. Idea: Elevator Prog
    By gamer4life687 in forum Contests Board
    Replies: 0
    Last Post: 11-15-2002, 10:01 PM
  5. password prog
    By ihsir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-06-2002, 06:39 AM