473,789 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing more than value using pointer

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_we ek );

The Function:
/*---------*/
weeks_rental(cu st,due,re_per_w eek) /*---determine rent per week*/
/*------------------------------*/
struct customer_record *cust;
float *due;
float *re_per_week;
{
/*rent per week*/
switch ((*cust).tv_typ e)
{
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_su mm;
float rent_out;
float rent_per_week,r entdue;
/*1-open files-Old master(read),te mporary 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",&c ustomer.custome r_no,&customer. no_of_weeks,&cu stomer.tv_type) ;

/*

/*2-2-process bill*/
/*----------------*/
/*2-2-1 if not of file record*/
/*---------------------------*/
if (customer.custo mer_no!=9999)
{
/*2-2-1-1 determine rent due*/
/*--------------------------*/
weeks_rental ( & customer , & rentdue , &rent_per_we ek );
/*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=(rentd ue+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(&cus tomer,rent_per_ week,rentdue);
}
/*2-End of while not at end of file master file*/
/*--------------------------------------*/
} while (customer.custo mer_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(cu st,due,re_per_w eek) /*---determine rent per week*/
/*------------------------------*/
struct customer_record *cust;
float *due;
float *re_per_week;
{
/*rent per week*/
switch ((*cust).tv_typ e)
{
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).cus tomer_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\nCus tomer 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.cu stomer_no,&cust .no_of_weeks,&c ust.tv_type,&re nt);

/*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,ren t_due);

/*calcualte total rent due*/
total=total+ren t_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);
}

Nov 14 '05 #1
3 1518
On 12 Jun 2005 07:57:15 -0700, eh**********@gm ail.com wrote:
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.
weeks_rental(c ust,due,re_per_ week) /*---determine rent per week*/
/*------------------------------*/
struct customer_record *cust;
float *due;
float *re_per_week;
{
/*rent per week*/
switch ((*cust).tv_typ e)
{
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 :
printf("\n Program does not handle this type of rental\n");
return(0);
}
}


Nov 14 '05 #2
eh**********@gm ail.com wrote:
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(st ruct customer_record *cust, double *due,
double *re_per_week);
void print_bill(stru ct customer_record *cust, double rent, double due);
void summary(void);

double round_to_cent(d ouble 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.tx t", "r")) == NULL) {
fprintf(stderr, "Can not open file 'setup.txt' for reading \n"
"Program is terminated.");
exit(EXIT_FAILU RE);
}
if ((fp_summ = fopen("summ.txt ", "w")) == NULL) {
fprintf(stderr, "Can not open file 'summ' for writing \n"
"Program is terminated.");
exit(EXIT_FAILU RE);
}

/* get data */
while (fgets(buf, sizeof buf, fp_rent)) {
if ((nl = strchr(buf, '\n')))
*nl = 0;
if (4 != sscanf(buf, "%4d%2d%c", &customer.custo mer_no,
&customer.no_of _weeks, &customer.tv_ty pe)) {
fprintf(stderr, "The input line\n \"%s\"\nis ill-formed.\n"
"Program is terminated.", buf);
exit(EXIT_FAILU RE);
}

/* and process the bill */
if (customer.custo mer_no == C_SENTINAL)
break;
weeks_rental(&c ustomer, &rentdue, &rent_per_week) ;
rent_out = round_to_cent(r entdue);
fprintf(fp_summ , "%4d %2d %c %6.2f\n", customer.custom er_no,
customer.no_of_ weeks, customer.tv_typ e, rent_out);
if (rent_per_week)
print_bill(&cus tomer, rent_per_week, rentdue);
}
fclose(fp_rent) ;
fclose(fp_summ) ;
return 0;
}
void weeks_rental(st ruct 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(stru ct 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);
}

Nov 14 '05 #3
eh**********@gm ail.com wrote:
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(st ruct customer_record *cust, double *due,
double *re_per_week);
void print_bill(stru ct customer_record *cust, double rent, double due);
void summary(void);

double round_to_cent(d ouble 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.tx t", "r")) == NULL) {
fprintf(stderr, "Can not open file 'setup.txt' for reading \n"
"Program is terminated.");
exit(EXIT_FAILU RE);
}
if ((fp_summ = fopen("summ.txt ", "w")) == NULL) {
fprintf(stderr, "Can not open file 'summ' for writing \n"
"Program is terminated.");
exit(EXIT_FAILU RE);
}

/* get data */
while (fgets(buf, sizeof buf, fp_rent)) {
if ((nl = strchr(buf, '\n')))
*nl = 0;
if (4 != sscanf(buf, "%4d%2d%c", &customer.custo mer_no,
&customer.no_of _weeks, &customer.tv_ty pe)) {
fprintf(stderr, "The input line\n \"%s\"\nis ill-formed.\n"
"Program is terminated.", buf);
exit(EXIT_FAILU RE);
}

/* and process the bill */
if (customer.custo mer_no == C_SENTINAL)
break;
weeks_rental(&c ustomer, &rentdue, &rent_per_week) ;
rent_out = round_to_cent(r entdue);
fprintf(fp_summ , "%4d %2d %c %6.2f\n", customer.custom er_no,
customer.no_of_ weeks, customer.tv_typ e, rent_out);
if (rent_per_week)
print_bill(&cus tomer, rent_per_week, rentdue);
}
fclose(fp_rent) ;
fclose(fp_summ) ;
return 0;
}
void weeks_rental(st ruct 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(stru ct 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);
}
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
36414
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
58
10182
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
8
4117
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
11
4471
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
17
2813
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to free()? #include <stdlib.h> struct stype { int *foo; };
12
5404
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
7
3307
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
8
3506
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
11
6170
by: =?Utf-8?B?U3VqZWV0?= | last post by:
If there are long strings (like 1MB or 2MB) is it more performant to pass those by ref to methods or by value?
5
1479
by: CapCity | last post by:
I'm sure I'm missing something simple - I do not code in C regularly, and the breaks are long enough for me to forget. The situation I have is I need to create an array but I do not know the dimension until runtime. I pass the pointer to a function which then determines the size and then creates and populates it. I can walk the array OK in the function, but the app crashes when I try to do so in the calling routine. Here's a small...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10412
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.