Hi all,

Im currently doing an assignment where we have been told to write a program to input 2 values (namely the area of wallspace to be painted & the price of the paint) & then output several other values based on the values input.

At this point I'm writing a function (determineLabourAndPaint) that is going to calculate the litres of paint required & the hours of labour that will be needed to perform the job.

However, I can't get the litres of paint to round up to the next integer value (I've put code in that, according to the textbook, is correct for performing such a task).

The code I've got so far is:

//Assignment 2 Q5a          
#include<iostream>
using namespace std;

const int COST_UNIT = 8;
const float LABOUR_FACTOR = 1.5;
const int LABOUR_COST_PER_HOUR = 55;

void inputAndValidate (float & wallSpaceP, float & paintCost)
{
    cout << "Enter the WallSpace (in metres squared): ";
    cin >> wallSpaceP;
    cout << "Enter the price per litre of the paint: ";
    cin >> paintCost; 
    if (paintCost < 20.00)
    do
    {
        cout << "The price per litre of the paint can't be less than R20.00." 
             << endl << "Please enter the price again: R";
        cin >> paintCost;
    }while (paintCost < 20.00);
}

void determineLabourAndPaint (float wallSpaceP, float & hoursLab,
                                               int & litresPaintReq)
{
    float paintLitres;
    paintLitres = (wallSpaceP / COST_UNIT) ;
    litresPaintReq = int (paintLitres + 0.5);
    hoursLab = ((wallSpaceP / COST_UNIT) * LABOUR_FACTOR);
}

int main ( )
{
    float wallSpace, paintPrice, hrsOfLabour, costOfLabour, costOfPaint, 
          totalCost;
    int litresPaint;
    
    inputAndValidate (wallSpace, paintPrice);
    determineLabourAndPaint (wallSpace, hrsOfLabour, litresPaint);
    cout.setf(ios::fixed);
    cout.precision(2);
    cout << "Wall space: " << wallSpace << " square metres\n"
         << "Price of paint: R" << paintPrice << endl
         << "Hours of labour: " << hrsOfLabour << " hours \n"
         << "Litres of paint: " << litresPaint << " litres \n"
         << "Cost of labour: R" << costOfLabour << endl
         << "Cost of paint: R" << costOfPaint << endl
         << "Total cost of paint job: R" << totalCost << endl;
    
         
    return 0;
}

Can anyone please assist me with just this part of the program??

Edited 6 Years Ago by BrandonB: n/a

If your paintLitres has a decimal of less than 0.5 it's going to round "down" (really just truncate off the decimal). Try using the ceil() function ( #include <cmath> ).

If your paintLitres has a decimal of less than 0.5 it's going to round "down" (really just truncate off the decimal). Try using the ceil() function ( #include <cmath> ).

We haven't been exposed to that function as of yet, & the problem I'm having is that I need the value to round up (say you need exactly 13.2 litres of paint for a room, you're not going to buy that exact amount, you're going to buy 14 litres because too little paint won't cover the whole room)

I've palyed around with the code for quite a while, but just can't seem to get it right

Edited 6 Years Ago by BrandonB: n/a

Yeah, I get the idea. You can emulate ceil() by casting paintLitres to an int and subtracting that value from the paintLitres value. If that difference is greater than zero litresPaintReq = (int) paintLitres +1; .
Since this involves floating point and "0" becomes somewhat subjective, you should set a threshold for comparison so say if the difference between paintLitres and (int) paintLitres is greater than 0.001 then round up (since you can probably give or take a ml of paint).

Edited 6 Years Ago by jonsca: n/a

I've now done this to the function

determineLabourAndPaint

, It does the job as far as I can see, but do you think it will be acceptable or not?

void determineLabourAndPaint (float wallSpaceP, float & hoursLab,
                                               int & litresPaintReq)
{
    float difference;
    float paintLitres;
    paintLitres = (wallSpaceP / 8);
    litresPaintReq = int (paintLitres);
    for (int i = 1; i <= 2; i++)
    {
        difference = 0.5 - (litresPaintReq - paintLitres);
        paintLitres += difference;
        litresPaintReq = int (paintLitres);
    }
         
    hoursLab = ((wallSpaceP / COST_UNIT) * LABOUR_FACTOR);
}

Edited 6 Years Ago by BrandonB: n/a

It really isn't possible to cover every function/construct in the language in a single semester. I'm sure your instructor will not balk at the use of ceil(). A big part of programming classes is researching the language by digging into documentation and experimenting with new constructs.

Unless they specifically say to not use a construct, it's fair game. Use of a different construct shows effort and instructors like that.

A hand-trace seems to indicate that the method works, but I only did it once. There may be a situation under which it doesn't work.

Edited 6 Years Ago by Fbody: n/a

Well it's more than it needs to be:

if (paintLitres - (int) paintLitres > 0.001)
     litresPaintReq = (int) paintLitres+1;
else
     litresPaintReq = (int) paintLitres;

You'll go back and look at yours 6 months from now and have to scratch your head for a minute.

Thank-you, I'm going to go put that fragment into my program now (i.e. try to figure out where it goes) & if I get stuck I'll come ask for help again...

I appreciate your help.

Nevermind that, I figured it out without any problem...

Thank-you very much.

This question has already been answered. Start a new discussion instead.