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*/