Thread: Functions/Sub functions

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    54

    Functions/Sub functions

    Hi, getting two small errors in lines 95 and 97 with semicolon but everything looks fine.
    The issue is that my program works fine without the "calc" and "printResult" functions but these are required as functions to my sub functions so I am trying to implement them. Here is what I have.

    Code:
    /* Creates customer's bills given the length and the width of the carpet in feet,
       the carpet price per square foot and the percent of discount for each customer.
            Written by: 
            Date:       10/9/14
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    
    //Constants
    #define TAX 8.5
    #define LABOR 0.35
    
    
    //Function Declarations
    int readInput (int* length, int* width,
                   float* percent_discount, float* cost);
    void installedPrice(int length, int width, float cost,
                        int* area, float* carpet_cost, float* labor_cost);
    void calc           (int length, int width, float cost,
                       int* area, float* carpet_cost,
                       float* labor_cost,
                       float percent_discount, float* subtotal,
                       float* discount, float* carpetandlabor,
                       float* total_price, float* total_tax);
    void calcSubtotal(float carpet_cost, float labor_cost, float percent_discount,
                      float* subtotal, float* discount, float* carpetandlabor);
    void calcTotal(float subtotal, float discount,
                   float* total_price, float* total_tax);
    void printResult (int length, int width, int area, float cost, float percent_discount,
                      float carpet_cost, float labor_cost, float subtotal, float discount,
                      float total_price, float total_tax, float carpetandlabor);
    void printMeasurements(int length, int width, int area);
    void printCharges(float cost, float percent_discount, float carpet_cost, float labor_cost,
                      float subtotal, float discount, float total_price, float total_tax, float carpetandlabor);
    
    
    int main()
    {
    //Variables
        int length;
        int width;
        float percent_discount;
        float cost;
    
    
    //Calculated variables
        int area;
        float carpet_cost;
        float labor_cost;
        float subtotal;
        float discount;
        float total_price;
        float total_tax;
        float carpetandlabor;
    
    
    //Statements
        readInput(&length, &width, &percent_discount, &cost);
        installedPrice(length, width, cost, &area,
                       &carpet_cost, &labor_cost);
        calcSubtotal(carpet_cost, labor_cost, percent_discount,
                    &subtotal, &discount, &carpetandlabor);
        calcTotal(subtotal, discount, &total_price, &total_tax);
        printMeasurements(length, width, area);
        printCharges(cost, percent_discount, carpet_cost, labor_cost,
                     subtotal, discount, total_price, total_tax, carpetandlabor);
    
    
        return 0;
    }//main
    
    
    /*=========================readInput========================================
      This function prompts and reads input entered at the keyboard and stores the input
      in the proper memory address.
    */
    int readInput (int* length, int* width, float* percent_discount, float* cost)
    {
        printf("\nEnter length in feet (integer): ");
        scanf("%d", length);
        printf("\nEnter width in feet (integer): ");
        scanf("%d", width);
        printf("\nEnter discount rate (float): ");
        scanf("%f", percent_discount);
        printf("\nEnter cost per sq. ft. (float): ");
        scanf("%f", cost);
    
    
        return 0;
    }//readInput
    
    
    /*============================calc======================================
      This function encompasses all sub functions for the calculations of
      user input data to produce measurements and costs.
    */
    void calc          (int length, int width, float cost,
                       int* area, float* carpet_cost, float* labor_cost,
                       float percent_discount, float* subtotal,
                       float* discount, float* carpetandlabor,
                       float* total_price, float* total_tax)
    {
    //Statements
        calc installedPrice(length, width, cost, area, carpet_cost, labor_cost);
        calc calcSubtotal(carpet_cost, labor_cost, percent_discount,
                          subtotal, discount, carpetandlabor);
        calc calcTotal(subtotal, discount, total_price, total_tax);
    
    
        return;
    }//calc
    /*==========================installedPrice=======================
      This function calculates area and costs of carpet installation per square foot
      and labor per square foot.
    */
    void installedPrice(int length, int width, float cost, int* area,
                        float* carpet_cost, float* labor_cost)
    {
    //Statements
        *area = length * width;
        *carpet_cost = *area * cost;
        *labor_cost = *area * LABOR;
    
    
        return;
    }//installedPrice
    
    
    /*===========================calcSubtotal=====================================
      This function calculates by calculating variables and user input into a price
      that can be output.
    */
    void calcSubtotal(float carpet_cost, float labor_cost, float percent_discount,
                      float* subtotal, float* discount, float* carpetandlabor)
    {
    //Statements
        *carpetandlabor = carpet_cost + labor_cost;
        *discount = ((percent_discount/100) * *carpetandlabor);
        *subtotal = *carpetandlabor - *discount;
    
    
    
    
        return;
    }//calcSubtotal
    
    
    /*==========================calcTotal==============================================
      This function calculates the total price of installation including labor, material
      costs, applied dousounts, and tax.
    */
    void calcTotal(float subtotal, float discount, float* total_price, float* total_tax)
    {
    //Statements
        *total_tax = ((TAX/100) * subtotal);
        *total_price = *total_tax + subtotal;
    
    
        return;
    }//calcTotal
    
    
    /*====================printResult=================================
      This function encompasses the sub functions to print user input
      and print calculated output.
    */
    void printResult (int length, int width, int area, float cost, float percent_discount,
                      float carpet_cost, float labor_cost, float subtotal, float discount,
                      float total_price, float total_tax, float carpetandlabor)
    {
    //Statements
       printMeasurements (length, width, area);
       printCharges  (cost, percent_discount, carpet_cost,
                     labor_cost, subtotal, discount,
                      total_price, total_tax, carpetandlabor);
    
    
        return;
    
    
    }//printResult
    
    
    /*=============printMeasurements======================
      This function  prints the length, width, and area
      entered by the user.
    */
    void printMeasurements(int length, int width, int area)
    {
    //Statements
        printf("\n\t\tMEASUREMENTS\t\t");
        printf("\nLength\t%d ft.",length);
        printf("\nWidth\t%d ft.",width);
    
    
        return;
    }//printMeasurements
    
    
    /*=============================printCharges=============================
      This function prints the output of the calculations input by the user
      through the use implementation of calculation functions.
    */
    void printCharges(float cost, float percent_discount, float carpet_cost,
                      float labor_cost, float subtotal, float discount,
                      float total_price, float total_tax, float carpetandlabor)
    {
    //Statements
        printf("\n\t\tCHARGES\t\t");
        printf("\nDESCRIPTION\tCOST PER SQ.FT.\t\tCHARGE");
        printf("\n-----------\t---------------\t\t------");
        printf("\nCarpet\t\t\t%.2f\t\t$%.2f",cost, carpet_cost);
        printf("\nLabor\t\t\t%.2f\t\t$%.2f",LABOR, labor_cost);
        printf("\n\t\t\t\t\t------");
        printf("\nInstalled Price\t\t\t\t$%.2f",carpetandlabor);
        printf("\nDiscount\t\t%.2f%\t\t$%.2f",percent_discount, discount);
        printf("\n\t\t\t\t\t------");
        printf("\nSubtotal\t\t\t\t$%.2f", subtotal);
        printf("\nTax\t\t\t\t\t$%.2f",total_tax);
        printf("\nTotal\t\t\t\t\t$%.2f",total_price);
    
    
        return;
    }//printCharges

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    133
    Can you post your error?

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    I got it, it was parameter error and me putting calc in front of statements in calc function. Thank you.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You might also want to get over the notion of "sub function". There is no such thing in C.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    I got it, it was parameter error and me putting calc in front of statements in calc function. Thank you.
    What you meant to say was "I got my answer here, it was parameter error and me putting calc in front of statements in calc function."


    Jim

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    Haha, yes Jim, I was trying to take all the credit for this incredibly complex beginner C prog in order to impress all. I realize it is not good from to post duplicates.

    Anyway, I got my answer there, then from the compiler output. Thanks.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    I realize it is not good from to post duplicates.
    Then why are you still doing it? Pick one forum and stick with it.

    Jim

  8. #8
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    Quote Originally Posted by jimblumberg View Post
    Then why are you still doing it? Pick one forum and stick with it.

    Jim
    will do

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  2. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  3. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM