Help with pointers and members of structures

This is a discussion on Help with pointers and members of structures within the C Programming forums, part of the General Programming Boards category; I'm having trouble referencing a member with a structure. The program is suppose to check to see if some stock ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Help with pointers and members of structures

    I'm having trouble referencing a member with a structure. The program is suppose to check to see if some stock is available and if it is, it the multiplies the price of the item by the quantity required. In the full program this multiplication is incorrect, so I've put in dummy lines to print the quantity and the price. The quantity prints correctly, but the price is incorrect. Can any one suggest where i've gone wrong.

    Note this is not the full Program


    #include <ISAM.H>


    struct ir_records{
    char rec_type[2],
    cust_code[6],
    part_no[7],
    quantity[5];

    };

    struct creat_records{
    char rec_type[2],
    cust_code[6],
    cust_name[21],
    cust_address[61],
    cust_balance[10],
    cust_limit[8];
    };

    struct delete_records{
    char rec_type[2],
    cust_code[6];
    };

    union records{
    struct ir_records ir;
    struct creat_records create;
    struct delete_records del;
    } trans;

    typedef struct{
    char cust_code[6];
    char cust_name[21];
    char cust_addr[61];
    float cust_bal;
    long credit_limit;
    int year,
    month,
    day;
    } MASTER_REC, NMR;


    void read_trans_file(void);
    void read_master_file(void);




    /*file pointers*/
    FILE *sorted_file,
    *old_master_file,
    *stock_file,
    *new_master_file,
    *error_file;

    /*Gobal varibles*/

    RECORD_DATA *stock_rec, stock;
    MASTER_REC master_ptr;
    NMR new_ptr;


    void main()
    {

    /*open files*/
    if ((sorted_file = fopen("KL09sd.DAT","rb")) == NULL)
    {
    printf("\n Sorted file not available - exiting program");
    exit(1);
    }


    if ((stock_file = fopen("STCKMAST.DAT","rb+")) == NULL)
    {
    printf("\n Error in opening stock file - exiting program");
    exit(1);
    }
    if ((error_file = fopen("print.txt","w")) == NULL)
    {
    printf("\n Error in opening print file - exiting program");
    exit(1);
    }


    read_trans_file(); /*read data from both file to start with*/
    read_master_file();

    /* main literation, which is performed until
    both files have reached the end of their files.
    */
    while ((strcmp(trans.ir.cust_code, "XXXXX")) || (strcmp(master_ptr.cust_code, "XXXXX") )!=0)
    {

    if (strcmp(trans.ir.cust_code, master_ptr.cust_code) <0)
    { /*The transaction customer number is less that the */
    option = 1; /*current master number then it is a new customer */
    }
    else
    {
    if (strcmp(trans.ir.cust_code, master_ptr.cust_code) >0)
    { /*The transaction customer number is greater than the */
    option = 2; /*current customer number */
    }
    else
    {
    option = 3;
    }
    }

    switch(option)
    {

    case 3: /*The two customer code are equal - 4 choices */
    /*1. Create a new customer which is invalid */
    /*2. Issue from stock */
    /*3. Return to stock */
    /*4. Delete a customer */

    switch(*trans.ir.rec_type)
    {
    case 'R': if ((stock_rec = search_file(trans.ir.part_no)) != NULL)
    {
    fprintf(error_file,"\nbalance = %7.2f",master_ptr.cust_bal);
    fprintf(error_file,"\nqauntity = %d , price = %4.2f", atoi (trans.ir.quantity),stock.price);

    master_ptr.cust_bal +=(atoi(trans.ir.quantity))* (stock_rec->price);
    stock_rec->free_stock +=atoi(trans.ir.quantity);
    }
    break;


    }/*end switch*/

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    hi klawton, I am at work at the moment, so I do not have the program here. If I remember correctly, you will have to break this sequence up like so.....

    double quantity;

    quantity = ((double)atol(record.qty)) * 100.00;
    balance += quantity;

    this may be incorrect, basically you will have to use atol instead of atoi, then cast it to a double or float, then * 100.00 to move the decimal point two places. All that is required then is to add or subtract the quantity depending whether record type is I or R.

    Shall look this up when I get home, and let you know.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi again at home now , here is my function to perform the same calculation. Ignore the previous post.

    my function to perform the same routine
    Code:
    /* function to perform the issue and return of stock - parametrs are a pointer 
        to the current record of the typedef union ALLRECS and pointer to the
        typedef struct MASTER_REC - RETURN VALUE is 0 if search for the
        part number in the stock master file is successful, 1 if unsuccessful*/
    int check_issue_return(ALLRECS *trans,    MASTER_REC *oldcust)
    {
    	RECORD_DATA *stock_rec;  /* pointer to the typedef struct RECORD_DATA */
    	int quantity;  /* to store the int value of string quantity from ALLRECS record */
    
    	if((stock_rec = search_file(trans->Iss_Ret.part_no))!=NULL)
    	{
    	  quantity = atoi(trans->Iss_Ret.qty);
    
    	  if(trans->Iss_Ret.rec_type =='I') {
    		/* cast quantity as floating point value then add to cust bal
     		   of master file */
    
    		oldcust->cust_bal += ((float)quantity * stock_rec->price);
    	  }
    	  else  {
    		/* cast quantity as floating point value then subtract from cust bal
     		   of master file */
    
    		oldcust->cust_bal -= ((float)quantity * stock_rec->price);
    	  }
    
    	  return 0;
    	}
    	else
    	  return 1;
    
    } /* end check_issue_return */
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers as data members
    By Drogin in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2009, 05:55 AM
  2. Using pointers to access variables in structures
    By Desolation in forum C++ Programming
    Replies: 5
    Last Post: 07-27-2006, 12:10 PM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. pointers as members of classes
    By punkrockguy318 in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2003, 05:35 PM
  5. Accessing members of structures
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 02-03-2002, 11:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21