Passing more than value using pointer

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);
}
 
P

Paul Mesken

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 .

You forgot the breaks in the switch statement.
 
M

Martin Ambuhl

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 .

The most obvious error is in lines like
> fprintf(fp_summ,"%4d %2d %c %6d\n",customer,rent_out);
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;
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:
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);
}
 
M

Martin Ambuhl

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 .

The most obvious error is in lines like
fprintf(fp_summ,"%4d %2d %c %6d\n",customer,rent_out);
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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top