I’m very new to this, so please bear with me. For a coding C class, we’re to use a total of nine functions and a header file (we’re calling it my.h) to collect the length and width of a room (ints), the percent discount (also an int), and the unit price of the carpet (a double). We’re to use the input to calculate the total cost to install the carpet. I can get the length, width and area to print, but not the unit price. It prints frin from the Read_Data.c and the Calc_Value.c functions, but not in Install_Price.c, which is called in Calc_Value.c. unit_price is read at the same time as length and width, but somehow, its not passed correctly. I’m not sure why. Maybe it does require a different pointer. Any help would be immensely appreciated. I’ve read my book and spoken to my professor and classmates, as well as searched the Internet, but I haven’t found anything that helps. The code for Install_Price is:
/*This function calculates the cost of the carpet and the labor cost in order to
calculate the installed price.*/
#include "my.h"
void Install_Price (int length, int width, int unit_price, int* area,
double* carpet_cost, double* labor_cost, double* installed_price)
{
printf("\nThe unit price is %7.2f.\n", *unit_price);
*area = length * width;
*carpet_cost = (*area) * unit_price;
printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price, *carpet_cost);
*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);
return;
}
Please note, the printf statements here are just to try and figure out where I went wrong with unit_price. Below, I included the my.h header code, main.c, and the functions that it calls upto Install_Price.c. Again, thanks for all your help!
my.h
#include <stdio.h>
void Read_Data(int* length, int* width, int* percent_discount, double*
unit_price);
void Calc_Values(int length, int width, int percent_discount, double
unit_price, int* area, double* carpet_cost, double* labor_cost, double*
installed_price, double* discount, double* subtotal, double* tax,
double* total);
void Install_Price(int length, int width, int unit_price, int* area, double*
carpet_cost, double* labor_cost,
double* installed_price);
void Subtotal (int percent_discount, double installed_price, double* discount,
double* subtotal);
void Total (double subtotal, double* tax, double* total);
void Print (int length, int width, int area, double unit_price, double carpet_cost,
double labor_cost, double
installed_price, int percent_discount, double discount, double subtotal, double tax,
double total);
void Print_Measurements (int length, int width, int area);
void Print_Charges (double unit_price, double carpet_cost, double labor_cost, double
installed_price, int percent_discount, double discount, double subtotal, double tax,
double total);
#define LABOR_RATE 0.35
#define TAX_RATE 0.085
main.c
/* This function calls three subfuctions to calculate the costs of installing a
carpet and prints an invoice. */
#include "my.h"
int main (void)
{
int length;
int width;
int percent_discount;
double unit_price;
int area;
double carpet_cost;
double labor_cost;
double installed_price;
double discount;
double subtotal;
double tax;
double total;
Read_Data(&length, &width, &percent_discount, &unit_price);
Calc_Values(length, width, percent_discount, unit_price, &area, &carpet_cost,
&labor_cost,&installed_price, &discount, &subtotal, &tax, &total);
Print(length, width, area, unit_price, carpet_cost, labor_cost,
installed_price, percent_discount, discount, subtotal, tax, total);
return 0;
}
Read_Data.c
/*This function asks the user for the length and width of a room to be carpeted, the
percent discount and the unit price of the carpet. */
#include "my.h"
void Read_Data (int* length, int* width, int* percent_discount, double* unit_price)
{
printf("What is the length, in feet, of the room?\n");
scanf("%d", length);
printf("What is the width, in feet, of the room?\n");
scanf("%d", width);
printf("What is the percent discount?\n");
scanf("%d", percent_discount);
printf("What is the unit price of the carpet?\n");
scanf("%lf", unit_price);
printf("\nThe length is %6d.\n", *length); //These printf statements work properly.
printf("The width is %6d.\n", *width);
printf("The percent discount is %3d%.\n", *percent_discount);
printf("The unit price is $%7.2f.\n", *unit_price);
return;
}
Calc_Value.c
/*This function calls three subfuctions that calculate all required quantities. */
#include "my.h"
void Calc_Values (int length, int width, int percent_discount, double unit_price,
int* area, double* carpet_cost, double* labor_cost, double* installed_price, double*
discount, double* subtotal, double* tax, double* total)
{
printf("\nUnit Price: %7.2f.\n", unit_price); //This printf statement works properly.
Install_Price (length, width, unit_price, area, carpet_cost, labor_cost,
installed_price);
Subtotal (percent_discount, *installed_price, discount, subtotal);
Total (*subtotal, tax, total);
return;
}
Install_Price.c (repeating for user ease)
/*This function calculates the cost of the carpet and the labor cost in order to
calculate the installed price.*/
#include "my.h"
void Install_Price (int length, int width, int unit_price, int* area,
double* carpet_cost, double* labor_cost, double* installed_price)
{
printf("\nThe unit price is %7.2f.\n", *unit_price); //THIS DOES NOT WORK
*area = length * width;
*carpet_cost = (*area) * unit_price;
printf("The carpet cost is %7d x %7.2f = %7.2f.\n", *area, unit_price,
*carpet_cost); //THIS DOES NOT WORK
*labor_cost = (*area) * LABOR_RATE;
*installed_price = (*carpet_cost) + (*labor_cost);
return;
}
Your issue is that in the
Install_Pricefunction declaration unit_price is declared anint, but in all other function definitions it is declared adoubleor*double. C is automatically casting thedoubleto anintwhen you pass that value toInstall_Price. The issue is with these two lines of code inInstall_price:and
You are using
%fto print the unit price, which has been cast to anint.Changing the definition of
Install_PricetoShould solve one of your problems.
The other issue is also in this line:
unit_priceshould not be dereferenced in this line. Change this to:To fix that issue.