Please help - Program to Compute Cost

This is a discussion on Please help - Program to Compute Cost within the C++ Programming forums, part of the General Programming Boards category; Can someone please point out to me what I'm doing wrong . Below is my source code for a program ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    Please help - Program to Compute Cost

    Can someone please point out to me what I'm doing wrong . Below is my source code for a program I wrote to calculate the cost of carpet & installation given the length,width, discount and cost per sq. foot. Everything works fine except that it doesn't compute and less the discount amount. Someone please help.


    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std;
    
    const float labor = 0.35;
    const float tax = 0.085;
    
    void getData (int &length, int&width, int &discount, float &cost);
    void calculate (int length, int width, int discount, float cost, float &totalLabor, float &totalCarpet, float &installation, float &subtotal, float &total, float &taxamount, float &discountamount);
    void printResult(int length, int width,int discount,float cost,float totalLabor,float totalCarpet,float installation,float discountamount,float subtotal,float taxamount,float total);
    
    int main ()
    {
        int length,width,discount;
        float cost,totalLabor,totalCarpet,installation,subtotal,total,discountamount,taxamount;
    
        getData(length,width,discount,cost);
        calculate(length,width,discount,cost,totalLabor,totalCarpet,installation,subtotal,total,taxamount,discountamount);
        printResult(length,width,discount,cost,totalLabor,totalCarpet,installation,discountamount,subtotal,taxamount,total);
    
        system ("PAUSE");
        return EXIT_SUCCESS;
    }
    
    
    
    
    void getData(int &length, int &width, int &discount, float &cost)
    {
            cout << "Length of the room (feet)? " << endl;
            cin >> length;
            cout << "Width of the room (feet)? " << endl;
            cin >> width;
            cout << "Customer discount (percent)? " << endl;
            cin >> discount;
            cout << "Cost per square feet (xxx.xx)? " << endl;
            cin >> cost;
    }
    
    float calcInstall (int length, int width, float cost, float &totalLabor, float &totalCarpet)
    {
    
    totalLabor=length*width*labor;
    totalCarpet=length*width*cost;
    
    return totalLabor+totalCarpet;
    }
    
    float calcSubtotal (float installation,int discount,float &discountamount)
    {
    
    discountamount = installation*(discount/100);
    
    return installation-discountamount;
    }
    
    float calcTotal (float subtotal,float &taxamount,float &total)
    {
    taxamount=subtotal*tax;
    total=subtotal+taxamount;
    
    return total;
    }
    
    void calculate (int length, int width, int discount, float cost, float &totalLabor, float &totalCarpet, float &installation, float &subtotal, float &total, float &taxamount, float &discountamount)
    {
    
    installation=calcInstall(length,width,cost,totalLabor,totalCarpet);
    subtotal=calcSubtotal(installation,discount,discountamount);
    total=calcTotal(subtotal,taxamount,total);
    }
    
    
    void printMeasurement (int length,int width)
    {
            cout<<"                       THE BROOKLYN COLLEGE CARPET STORE" << endl;
    	cout    <<"                             ABDOOL RAHEEM, Owner" <<endl << endl << endl;
    	cout <<"                                  MEASUREMENT" << endl << endl;;
    	cout << right << setw(35) << "Length" << setw(10) << length << left << setw(34)
                <<" feet" << endl;
    	cout << right << setw(35) << "Width" << setw(10) <<width << left << setw(34)
                << " feet" << endl;
    	cout << right << setw(35) << "Area" << setw(10) << length * width << left
                << setw(34) <<" square feet" << endl << endl << endl;
    
    
    }
    
    void printCharges(float cost,float totalCarpet,float totalLabor,float installation,int discount,float discountamount,float subtotal,float taxamount,float total)
    {
        cout <<"                                    CHARGES"<<endl<<endl;
    	cout << left << setw(20) << "DESCRIPTION" << right << setw(20)
                << "COST/SQ FT" << setw(20) << "CHARGE/ROOM" << endl;
    	cout << left << setw(20) << "Carpet" << right << setw(20)<< fixed
                << setprecision(2) << cost << setw(20)
                << setprecision(2) << totalCarpet << endl;
    	cout << left << setw(20)<< "Labor" << right << setw(20) << "0.35"
                << setw(20) << setprecision(2) << totalLabor << endl << endl;
    	cout << left << setw(20) << "INSTALLED PRICE" << right << setw(20)
                << "" << setw(20) << setprecision(2) << installation << endl;
    	cout << left << setw(20) << "Discount" << right << setw(20) << ""
                << setw(20) << setprecision(2) << discountamount << endl;
    	cout << right << setw(60) << "----------" << endl;
    	cout << left << setw(20) << "Sub Total" << right << setw(20) << ""
                << setw(20) << setprecision(2) << subtotal << endl;
    	cout << left << setw(20) << "Tax" << right << setw(20) << "" << setw(20)
                << setprecision(2) << taxamount << endl;
    	cout << left << setw(20) << "Total" << right << setw(20) << "" << setw(20)
                << setprecision(2) << total << endl;
    
    }
    void printResult(int length, int width,int discount,float cost,float totalLabor,float totalCarpet,float installation,float discountamount,float subtotal,float taxamount,float total)
    {
        printMeasurement (length,width);
        printCharges (cost,totalCarpet,totalLabor,installation,discount,discountamount,subtotal,taxamount,total);
    }

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    The problem is in the function calcSubtotal:

    Code:
    float calcSubtotal (float installation, int discount, float& discountamount)
    {
    	discountamount = installation * (discount / 100);
    
    	return installation - discountamount;
    }
    The variable discount is an integer, so (discount / 100) returns 0 for anything less than 100. To fix this you can cast the discount variable to a float type, or simply change the type of the discount variable to a float.

    Code:
    float calcSubtotal (float installation, float discount, float& discountamount)
    {
    	...
    }
    
    // or
    
    float calcSubtotal (float installation, int discount, float& discountamount)
    {
    	discountamount = installation * ((float)discount / 100);
    
    	return installation - discountamount;
    }

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Be careful of integer division:
    Code:
    float calcSubtotal (float installation,int discount,float &discountamount)
    {
    
    discountamount = installation*(discount/100);
    
    return installation-discountamount;
    }
    (discount / 100) will always be 0 for any value of discount less than 100.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Hey,

    I got it working. Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# compute program
    By shady_shockerz in forum C# Programming
    Replies: 2
    Last Post: 02-10-2009, 07:43 PM
  2. Replies: 10
    Last Post: 03-13-2008, 12:04 PM
  3. How much would it cost...
    By SlyMaelstrom in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 08-17-2006, 05:24 PM
  4. cost of shrubbery
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-28-2003, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21