#C++ Freelancer Rates

1 messages · Page 1 of 1 (latest)

unkempt scarab
#

Hello again people, I've been trying to get my floats converted to integers, but I have a feeling that I am not supposed to be using something like static_cast to do so, so I have no idea where I should start looking as I've only been staring foolishly at my monitor hoping my second brain cell wakes up.

code currently looks like this:

#include <cmath>

// daily_rate calculates the daily rate given an hourly rate
double daily_rate(double hourly_rate) {
    return hourly_rate * 8.0;
}

// apply_discount calculates the price after a discount
double apply_discount(double before_discount, double discount) {
    return before_discount * (discount * 0.01);
}

// monthly_rate calculates the monthly rate, given an hourly rate and a discount
// The returned monthly rate is rounded up to the nearest integer.
int monthly_rate(double hourly_rate, double discount) {
    const double month_billable_days{22};
    return round(
        apply_discount(daily_rate(hourly_rate)* month_billable_days, discount)
        );
}

// days_in_budget calculates the number of workdays given a budget, hourly rate,
// and discount The returned number of days is rounded down (take the floor) to
// the next integer.
double days_in_budget(int budget, double hourly_rate, double discount) {
    // TODO: Implement a function that takes a budget, an hourly rate, and a
    // discount, and calculates how many complete days of work that covers.
    const double daily_rate_discounted{
        apply_discount(daily_rate(hourly_rate), discount)
    };
    const int budget_remainder{budget % static_cast<int>(daily_rate_discounted)};
    
    return floor(
        (budget - budget_remainder) / daily_rate_discounted
    );
}

error:

make[2]: *** [CMakeFiles/test_freelancer-rates.dir/build.make:70: CMakeFiles/test_freelancer-rates] Arithmetic exception (core dumped)
make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/test_freelancer-rates.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
#

and let's say I get rid of static_cast, this is what I get:

In file included from /tmp/freelancer-rates/freelancer_rates_test.cpp:1:
/tmp/freelancer-rates/freelancer_rates.cpp: In function 'double days_in_budget(int, double, double)':
/tmp/freelancer-rates/freelancer_rates.cpp:31:39: error: invalid operands of types 'int' and 'const double' to binary 'operator%'
   31 |     const int budget_remainder{budget % daily_rate_discounted};
      |                                ~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~
      |                                |        |
      |                                int      const double
make[2]: *** [CMakeFiles/freelancer-rates.dir/build.make:76: CMakeFiles/freelancer-rates.dir/freelancer_rates_test.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/freelancer-rates.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
#

I think I'll take a break before I get back to this

violet crown
#

please read the instruction carefully

#

in task 4, it said
The returned number of days should be rounded down (take the floor) to the next integer.

#

your function saying it should be returning double

#

double days_in_budget(int budget, double hourly_rate, double discount)

unkempt scarab
# violet crown your function saying it should be returning double

completely forgot about that, had it int before and changed it to double out of desperation (stupid, I know), anyway:

int days_in_budget(int budget, double hourly_rate, double discount) {
    // TODO: Implement a function that takes a budget, an hourly rate, and a
    // discount, and calculates how many complete days of work that covers.
    const int daily_rate_discounted{
        static_cast<int>(apply_discount(daily_rate(hourly_rate), discount))
    };
    const int budget_remainder{budget % daily_rate_discounted};
    
    return floor(
        (budget - budget_remainder) / daily_rate_discounted
    );
}

changed it to this now, getting this mysterious error message still though:

make[2]: *** [CMakeFiles/test_freelancer-rates.dir/build.make:70: CMakeFiles/test_freelancer-rates] Arithmetic exception (core dumped)
make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/test_freelancer-rates.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
#

I might be misunderstanding how static_cast works

violet crown
#

you dont really need static cast at any point. you could do everything in double and the final floor will make sure it is in int

#

also you should consider taking a look at the apply discount function and make sure if it is right

unkempt scarab
violet crown
#

round go to nearest interest, at .5 it moves away from 0, if you want specific round up or down you need to use either ceil or floor

unkempt scarab
#

oh.. it did say take the ceiling, that's straight up my bad

#

yup, solved now. Thank you for your time, help, and patience glaxxie.

#

spoilers for those who haven't solved the exercise yet:
||```cpp
#include <cmath>

// daily_rate calculates the daily rate given an hourly rate
double daily_rate(double hourly_rate) {
return hourly_rate * 8.0;
}

// apply_discount calculates the price after a discount
double apply_discount(double before_discount, double discount) {
return before_discount * ((100 - discount) * 0.01);
}

// monthly_rate calculates the monthly rate, given an hourly rate and a discount
// The returned monthly rate is rounded up to the nearest integer.
int monthly_rate(double hourly_rate, double discount) {
const double month_billable_days{22};
return ceil(
apply_discount(daily_rate(hourly_rate) * month_billable_days, discount)
);
}

// days_in_budget calculates the number of workdays given a budget, hourly rate,
// and discount The returned number of days is rounded down (take the floor) to
// the next integer.
int days_in_budget(int budget, double hourly_rate, double discount) {
// discount, and calculates how many complete days of work that covers.
const double daily_rate_discounted{
apply_discount(daily_rate(hourly_rate), discount)
};
// vvv You don't want to know vvv
//const int budget_remainder{budget % daily_rate_discounted};

return floor(
    budget / daily_rate_discounted
);

}