E
ehabaziz2001
I know one of the pointer benefit is that we can return more than one
value from a function . The down program has an error that I can not
discover . That is the call of the function :
The Call:
/*---------*/
weeks_rental ( & customer , & rentdue , &rent_per_week );
The Function:
/*---------*/
weeks_rental(cust,due,re_per_week) /*---determine rent per week*/
/*------------------------------*/
struct customer_record *cust;
float *due;
float *re_per_week;
{
/*rent per week*/
switch ((*cust).tv_type)
{
case 'c':
*due=(*cust).no_of_weeks*3.60;
*re_per_week=(3.60);
case 'b':
*due=(*cust).no_of_weeks*1.75;
*re_per_week=(1.75);
case 'v':
*due=(*cust).no_of_weeks*1.50;
*re_per_week=(1.50);
default :
printf("\n Program does not handle this type of rental\n");
return(0);
}
}
The Whole Program:
/*---------------*/
#include <stdio.h>
#include <stdlib.h>
/*TV rental system-Processing customer information from a
file-ver01-W*/
struct customer_record /*defining a customer record*/
{
int customer_no;
int no_of_weeks;
char tv_type;
};
main ()
{
struct customer_record customer;
FILE *fp_rent,*fp_summ;
float rent_out;
float rent_per_week,rentdue;
/*1-open files-Old master(read),temporary file(write)*/
/*---------------------------------------------------*/
if ((fp_rent=fopen("setup.txt","r")) == NULL)
{
printf("\nCan not open file 'setup.txt' for reading \n");
printf("Program is termainted");
exit(0);
}
if ((fp_summ=fopen("summ.txt","w")) == NULL)
{
printf("\nCan not open file 'summ' for writing \n");
printf("Program is termainted");
exit(0);
}
/*2-while not at end of file master file*/
/*--------------------------------------*/
do
{
/*2-1-read customers record from master file*/
/*-------------------------------------------*/
fscanf(fp_rent,"%4d%2d%c\n",&customer.customer_no,&customer.no_of_weeks,&customer.tv_type);
/*
/*2-2-process bill*/
/*----------------*/
/*2-2-1 if not of file record*/
/*---------------------------*/
if (customer.customer_no!=9999)
{
/*2-2-1-1 determine rent due*/
/*--------------------------*/
weeks_rental ( & customer , & rentdue , &rent_per_week );
/*2-2-1-2 Converting dut to integer ready for output
to the file. At this stage rounding
errors
may occur,hence the addition of 0.005*/
/*---------------------------------------------*/
rent_out=(rentdue+0.005)*100;
/*2-2-1-3 output information to summary file -
summ*/
/*-------------------------------------------------*/
fprintf(fp_summ,"%4d %2d %c %6d\n",customer,rent_out);
/*2-3-output customers bill on the screen for each record*/
/*--------------------------------------------------------*/
if (rent_per_week!=0)
print_bill(&customer,rent_per_week,rentdue);
}
/*2-End of while not at end of file master file*/
/*--------------------------------------*/
} while (customer.customer_no!=9999);
/*2-4-write customers bill information to temporary file-summ*/
/*-----------------------------------------------------------*/
fprintf(fp_summ,"%4d %2d %c %6d\n",customer,rent_out);
/*3-close files-masterfile-summ*/
/*-----------------------------*/
fclose(fp_rent);
fclose(fp_summ);
/*4-calculate and output summary table*/
/*summary();*/
}
weeks_rental(cust,due,re_per_week) /*---determine rent per week*/
/*------------------------------*/
struct customer_record *cust;
float *due;
float *re_per_week;
{
/*rent per week*/
switch ((*cust).tv_type)
{
case 'c':
*due=(*cust).no_of_weeks*3.60;
*re_per_week=(3.60);
case 'b':
*due=(*cust).no_of_weeks*1.75;
*re_per_week=(1.75);
case 'v':
*due=(*cust).no_of_weeks*1.50;
*re_per_week=(1.50);
default :
printf("\n Program does not handle this type of rental\n");
return(0);
}
}
print_bill(cust,rent,due)
/*------------------------------------------------------------------------*/
struct customer_record *cust;
float rent;
float due;
{
int k;
/*printf("\n TV RENTAL BILL No %d :",k);*/
printf("\n TV Rental bill" );
printf("\n ========================");
printf("\n\n Customer number is %d",(*cust).customer_no);
printf("\n Number of weeks rent due is %d",(*cust).no_of_weeks);
printf("\n Rent per week is %.2f ",rent);
printf("\n\n Rental due is %.2f \n\n",due);
}
summary() /*produce summary report*/
/*----------*/
{
struct customer_record cust;
float total,rent_due;
int rent;
FILE *fp_summ; /*Decalres fp_summ as a file pointer*/
total=0; /*initital total*/
/*open file-summ for read*/
if ((fp_summ=fopen("summ.txt","r")) == NULL)
{
printf("\nCan not open file 'summ' for reading \n");
printf("Program is termainted");
exit(0);
}
/*print table heading*/
printf("\n\n SUMMARY REPORT ");
printf("\n _____________");
printf("\n\nCustomer number Weeks due Rental type Rent due");
printf("\n--------------- --------- ----------- --------");
/*output summary table*/
do
{
/*read customer information from file summ*/
fscanf(fp_summ,"%4d %2d %c
%6d\n",&cust.customer_no,&cust.no_of_weeks,&cust.tv_type,&rent);
/*If end of file customer number is 9999*/
if (cust.customer_no!=9999)
{
/*Rent due was written to the file as an integer
therefore has to be converted back to a floating
point number*/
rent_due=rent/100.0;
/*print table of customer information*/
printf("\n %4d %2d %c
%6.2f",cust,rent_due);
/*calcualte total rent due*/
total=total+rent_due;
}
} while (cust.customer_no!=9999);
/*close files*
fclose(fp_summ);
/*print total rent due*/
printf("\n\n Total rent due is %12.2f \n\n",total);
}
value from a function . The down program has an error that I can not
discover . That is the call of the function :
The Call:
/*---------*/
weeks_rental ( & customer , & rentdue , &rent_per_week );
The Function:
/*---------*/
weeks_rental(cust,due,re_per_week) /*---determine rent per week*/
/*------------------------------*/
struct customer_record *cust;
float *due;
float *re_per_week;
{
/*rent per week*/
switch ((*cust).tv_type)
{
case 'c':
*due=(*cust).no_of_weeks*3.60;
*re_per_week=(3.60);
case 'b':
*due=(*cust).no_of_weeks*1.75;
*re_per_week=(1.75);
case 'v':
*due=(*cust).no_of_weeks*1.50;
*re_per_week=(1.50);
default :
printf("\n Program does not handle this type of rental\n");
return(0);
}
}
The Whole Program:
/*---------------*/
#include <stdio.h>
#include <stdlib.h>
/*TV rental system-Processing customer information from a
file-ver01-W*/
struct customer_record /*defining a customer record*/
{
int customer_no;
int no_of_weeks;
char tv_type;
};
main ()
{
struct customer_record customer;
FILE *fp_rent,*fp_summ;
float rent_out;
float rent_per_week,rentdue;
/*1-open files-Old master(read),temporary file(write)*/
/*---------------------------------------------------*/
if ((fp_rent=fopen("setup.txt","r")) == NULL)
{
printf("\nCan not open file 'setup.txt' for reading \n");
printf("Program is termainted");
exit(0);
}
if ((fp_summ=fopen("summ.txt","w")) == NULL)
{
printf("\nCan not open file 'summ' for writing \n");
printf("Program is termainted");
exit(0);
}
/*2-while not at end of file master file*/
/*--------------------------------------*/
do
{
/*2-1-read customers record from master file*/
/*-------------------------------------------*/
fscanf(fp_rent,"%4d%2d%c\n",&customer.customer_no,&customer.no_of_weeks,&customer.tv_type);
/*
/*2-2-process bill*/
/*----------------*/
/*2-2-1 if not of file record*/
/*---------------------------*/
if (customer.customer_no!=9999)
{
/*2-2-1-1 determine rent due*/
/*--------------------------*/
weeks_rental ( & customer , & rentdue , &rent_per_week );
/*2-2-1-2 Converting dut to integer ready for output
to the file. At this stage rounding
errors
may occur,hence the addition of 0.005*/
/*---------------------------------------------*/
rent_out=(rentdue+0.005)*100;
/*2-2-1-3 output information to summary file -
summ*/
/*-------------------------------------------------*/
fprintf(fp_summ,"%4d %2d %c %6d\n",customer,rent_out);
/*2-3-output customers bill on the screen for each record*/
/*--------------------------------------------------------*/
if (rent_per_week!=0)
print_bill(&customer,rent_per_week,rentdue);
}
/*2-End of while not at end of file master file*/
/*--------------------------------------*/
} while (customer.customer_no!=9999);
/*2-4-write customers bill information to temporary file-summ*/
/*-----------------------------------------------------------*/
fprintf(fp_summ,"%4d %2d %c %6d\n",customer,rent_out);
/*3-close files-masterfile-summ*/
/*-----------------------------*/
fclose(fp_rent);
fclose(fp_summ);
/*4-calculate and output summary table*/
/*summary();*/
}
weeks_rental(cust,due,re_per_week) /*---determine rent per week*/
/*------------------------------*/
struct customer_record *cust;
float *due;
float *re_per_week;
{
/*rent per week*/
switch ((*cust).tv_type)
{
case 'c':
*due=(*cust).no_of_weeks*3.60;
*re_per_week=(3.60);
case 'b':
*due=(*cust).no_of_weeks*1.75;
*re_per_week=(1.75);
case 'v':
*due=(*cust).no_of_weeks*1.50;
*re_per_week=(1.50);
default :
printf("\n Program does not handle this type of rental\n");
return(0);
}
}
print_bill(cust,rent,due)
/*------------------------------------------------------------------------*/
struct customer_record *cust;
float rent;
float due;
{
int k;
/*printf("\n TV RENTAL BILL No %d :",k);*/
printf("\n TV Rental bill" );
printf("\n ========================");
printf("\n\n Customer number is %d",(*cust).customer_no);
printf("\n Number of weeks rent due is %d",(*cust).no_of_weeks);
printf("\n Rent per week is %.2f ",rent);
printf("\n\n Rental due is %.2f \n\n",due);
}
summary() /*produce summary report*/
/*----------*/
{
struct customer_record cust;
float total,rent_due;
int rent;
FILE *fp_summ; /*Decalres fp_summ as a file pointer*/
total=0; /*initital total*/
/*open file-summ for read*/
if ((fp_summ=fopen("summ.txt","r")) == NULL)
{
printf("\nCan not open file 'summ' for reading \n");
printf("Program is termainted");
exit(0);
}
/*print table heading*/
printf("\n\n SUMMARY REPORT ");
printf("\n _____________");
printf("\n\nCustomer number Weeks due Rental type Rent due");
printf("\n--------------- --------- ----------- --------");
/*output summary table*/
do
{
/*read customer information from file summ*/
fscanf(fp_summ,"%4d %2d %c
%6d\n",&cust.customer_no,&cust.no_of_weeks,&cust.tv_type,&rent);
/*If end of file customer number is 9999*/
if (cust.customer_no!=9999)
{
/*Rent due was written to the file as an integer
therefore has to be converted back to a floating
point number*/
rent_due=rent/100.0;
/*print table of customer information*/
printf("\n %4d %2d %c
%6.2f",cust,rent_due);
/*calcualte total rent due*/
total=total+rent_due;
}
} while (cust.customer_no!=9999);
/*close files*
fclose(fp_summ);
/*print total rent due*/
printf("\n\n Total rent due is %12.2f \n\n",total);
}