Note that there are some explanatory texts on larger screens.

plurals
  1. POValue printed in one function works fine, but printed in the next function is wrong (returns 0.00)
    text
    copied!<p>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: </p> <pre><code>/*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; } </code></pre> <p>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!</p> <p>my.h</p> <pre><code>#include &lt;stdio.h&gt; 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 </code></pre> <p>main.c</p> <pre><code>/* 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(&amp;length, &amp;width, &amp;percent_discount, &amp;unit_price); Calc_Values(length, width, percent_discount, unit_price, &amp;area, &amp;carpet_cost, &amp;labor_cost,&amp;installed_price, &amp;discount, &amp;subtotal, &amp;tax, &amp;total); Print(length, width, area, unit_price, carpet_cost, labor_cost, installed_price, percent_discount, discount, subtotal, tax, total); return 0; } </code></pre> <p>Read_Data.c</p> <pre><code>/*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; } </code></pre> <p>Calc_Value.c</p> <pre><code>/*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; } </code></pre> <p>Install_Price.c (repeating for user ease)</p> <pre><code>/*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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload