Okay, slowly making progress in my book. I’m on the last ‘project’ of this chapter and I’ve f’ed it up lol. Seems like I made a bug I can’t find.
The issue:
The carpetCost variable is taking on a value I did not expect. carpetCost seems to be adding the value of laborCost when printed in the printCharges function.
I made sure to set the variable to 0 when I created it to make sure nothing else was being held there.
Checked the expression on the right of assignment operator for carperCost. Not adding laborCost at all.
When carpetCost is printed in my printCharges function it decides to add laborCost. Here are a couple outputs for your enjoyment.
\
Seems to me laborCost is being added to carpetCost somehow. So I investigate! When is the value of carpetCost getting wonky? Maybe it’s in the getCosts function or after getCosts has ran?
Okay, so everything within is fine.
What about just after getCosts finishes?
This is where I am lost. I can’t find my error. Another pair of eyes would be appreciated. Code is just below. Thanks!
Summary
#include <stdio.h>
#define laborPrice .35
#define taxRate .085
void getData ();
void calcValues ();
void getCosts (int* length, int* width, float* carpetPrice, float* area, float* laborCost, float* installedPrice, float* carpetCost);
void getSubtotal (float* installedPrice, floatdiscount, float subtotal, float* discountTotal);
void getTotal (float* subtotal, float* total, float* totalTax);
void printMeasures (int length, int width, float area);
void printCharges (float carpetPrice, float carpetCost, float laborCost, float installedPrice,
float discount, float subtotal, float discountTotal, float totalTax, float total);
int main (void)
{
calcValues();
return 0;
}
void calcValues()
{
int length = 0;
int width = 0;
float discount = 0;
float carpetPrice = 0;
float area = 0;
float laborCost = 0;
float installedPrice = 0;
float carpetCost = 0;
float subtotal = 0;
float total = 0;
float discountTotal = 0;
float totalTax = 0;
getData(&length, &width, &discount, &carpetPrice);
getCosts(&length, &width, &carpetPrice, &area, &laborCost, &installedPrice, &carpetCost);
getSubtotal(&installedPrice, &discount, &subtotal, &discountTotal);
getTotal(&subtotal, &totalTax, &total);
printMeasures (length, width, area);
printCharges (carpetPrice, carpetCost, laborCost, installedPrice, discount, subtotal, discountTotal, totalTax, total);
return;
}
void getData (int* length, int* width, float* discount, float* carpetPrice)
{
printf(“What is the length of the room? \n”);
scanf("%d", length);
printf(“What is the width of the room? \n”);
scanf("%d", width);
printf(“What percent discount is the customer receiving? \n”);
scanf("%f", discount);
printf(“What is the cost per square foot of the carpet? \n”);
scanf("%f", carpetPrice);
return;
}
void getCosts(int* length, int* width, float* carpetPrice, float* area, float* laborCost, float* carpetCost, float* installedPrice)
{
*area = *length * *width;
*laborCost = *area * laborPrice;
*carpetCost = *area * *carpetPrice;
*installedPrice = *laborCost + *carpetCost;
return;
}
void getSubtotal (float* installedPrice, float* discount, float* subtotal, float* discountTotal)
{
*discount = *discount / 100.0;
*discountTotal = *discount * *installedPrice;
*subtotal = *installedPrice - *discountTotal;
return;
}
void getTotal(float* subtotal, float* totalTax, float* total)
{
*totalTax = *subtotal * taxRate;
*total = *subtotal + *totalTax;
return;
}
void printMeasures (int length, int width, float area)
{
printf(" MEASUREMENTS\n");
printf(“length %d ft\n”, length);
printf(“width %d ft\n”, width);
printf(“Area %.2f sqft\n”, area);
return;
}
void printCharges (float carpetPrice, float carpetCost, float laborCost, float installedPrice,
float discount, float subtotal, float discountTotal, float totalTax, float total)
{
printf(" CHARGES\n");
printf(“DESCRIPTION COST/SQFT CHARGE\n”);
printf(“Carpet $%.2f $ %.2f\n”, carpetPrice, carpetCost);
printf(“labor Cost $ .35 $ %.2f\n”, laborCost);
printf(" ------\n");
printf(“Installation Cost $ %.2f\n”, installedPrice);
printf(“Discount %.1f%% $ %.2f\n”, discount * 100, discountTotal);
printf(“Subtotal $ %.2f\n”, subtotal);
printf(" ------\n");
printf(“Tax 8.5%% $ %.2f\n”, totalTax);
printf(“Total $ %.2f\n”, total);
}