ehabaziz2001@gmail.com wrote:[color=blue]
> 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 .[/color]
The most obvious error is in lines like[color=blue]
> fprintf(fp_summ,"%4d %2d %c %6d\n",customer,rent_out);[/color]
where customer is a struct. This obviously cannot work.
Your use of K&R-style function definitions without forward declarations
screams that you have modeled your code on some text dating from the
late 70s or early 80s, and your use of end-of-line characters to begin
lines smacks of old Borland (or worse, old Schildt) instruction
materials. To get you started, look at the following compiled but not
tested code. Whether it does what you want is unknown, but it should
point you toward writing slightly better code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define BIGENOUGH 2048
#define C_SENTINAL 9999
struct customer_record
{
int customer_no;
int no_of_weeks;
char tv_type;
};
void weeks_rental(struct customer_record *cust, double *due,
double *re_per_week);
void print_bill(struct customer_record *cust, double rent, double due);
void summary(void);
double round_to_cent(double x)
{
double b, f;
b = modf(100 * x, &f);
if (f >= .5)
b++;
return b / 100;
}
int main(void)
{
struct customer_record customer;
FILE *fp_rent, *fp_summ;
double rent_per_week, rentdue, rent_out;
char buf[BIGENOUGH], *nl;
/* Open files. Old master(read), temporary file(write) */
if ((fp_rent = fopen("setup.txt", "r")) == NULL) {
fprintf(stderr, "Can not open file 'setup.txt' for reading \n"
"Program is terminated.");
exit(EXIT_FAILURE);
}
if ((fp_summ = fopen("summ.txt", "w")) == NULL) {
fprintf(stderr, "Can not open file 'summ' for writing \n"
"Program is terminated.");
exit(EXIT_FAILURE);
}
/* get data */
while (fgets(buf, sizeof buf, fp_rent)) {
if ((nl = strchr(buf, '\n')))
*nl = 0;
if (4 != sscanf(buf, "%4d%2d%c", &customer.customer_no,
&customer.no_of_weeks, &customer.tv_type)) {
fprintf(stderr, "The input line\n \"%s\"\nis ill-formed.\n"
"Program is terminated.", buf);
exit(EXIT_FAILURE);
}
/* and process the bill */
if (customer.customer_no == C_SENTINAL)
break;
weeks_rental(&customer, &rentdue, &rent_per_week);
rent_out = round_to_cent(rentdue);
fprintf(fp_summ, "%4d %2d %c %6.2f\n", customer.customer_no,
customer.no_of_weeks, customer.tv_type, rent_out);
if (rent_per_week)
print_bill(&customer, rent_per_week, rentdue);
}
fclose(fp_rent);
fclose(fp_summ);
return 0;
}
void weeks_rental(struct customer_record *cust, double *due,
double *re_per_week)
{
/* rent per week */
switch (cust->tv_type) {
case 'c':
*due = cust->no_of_weeks * 3.60;
*re_per_week = 3.60;
break;
case 'b':
*due = cust->no_of_weeks * 1.75;
*re_per_week = 1.75;
break;
case 'v':
*due = cust->no_of_weeks * 1.50;
*re_per_week = 1.50;
break;
default:
fprintf(stderr,
"Program does not handle this type of rental\n");
}
}
void print_bill(struct customer_record *cust, double rent, double due)
{
printf("TV Rental bill\n"
" ========================\n\n"
" Customer number is %d\n"
" Number of weeks rent due is %d\n"
" Rent per week is %.2f\n\n" " Rental due is %.2f\n\n",
cust->customer_no, cust->no_of_weeks, rent, due);
}