[SOLVED] N00b needs C help, again

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.

getCosts

When carpetCost is printed in my printCharges function it decides to add laborCost. Here are a couple outputs for your enjoyment.

output1

output2\

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?

output3%20investigate

Okay, so everything within is fine.

What about just after getCosts finishes?

output%20investigate%204

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

1 Like

I didn’t check everything, but this cought my eye.

The getCosts function signature is:

Yet installedPrice and carpetCost are swapped in the call.

And since:

void getCosts(...)
{
    ...
    *carpetCost = *area * *carpetPrice;
    *installedPrice = *laborCost + *carpetCost;
    ...
}

let’s replace the vars as they are swapped.

    *installedPrice = *area * *carpetPrice;
    *carpetCost = *laborCost + *installedPrice;

Seems like that’s what it does indeed.


On a side note, you can use ```to denote code blocks

Like so:
```
int main(...)
```

And it will become:

int main(...)

After some sleep, I wanted to add this. In this case where you have tons of variables to pass. It’s a good idea to make a struct out of them. Also there is no reason to pass values that won’t be written to as pointers. It’s not a big struct that would waste time being copied. It’s just a few ints.

2 Likes

There it is…lol wow.

The one thing I’ve noticed about programming, up to this point, at least for me, no matter the planning it’s the little things that cause big hold ups XD

@oldgek Thanks for that. Everything seems to work fine now. All I have to do is get everything to line up nicely in the output and I’m ready for the next chapter.

3 Likes

It’s also a good example to practice using gdb or lldb if you’re a fan

1 Like