+ 1

Kaleidoscope code coach c++

Iā€™m close but I canā€™t seem round to the 2nd decimal properly #include <iostream> #include <iomanip> using namespace std; int main() { int order; cin >> order; float total = order * 5; float totalAfter; float discount =total - (total * 0.1); if (order > 1) { totalAfter = discount + (discount * 0.07); printf("%.2f", totalAfter); } else { totalAfter = total + (total * 0.07); printf("%.2f", totalAfter); } return 0; } I also used fixed << setprecision But that didnā€™t work either.

8th Mar 2022, 1:07 AM
zack
13 Answers
+ 4
HungryTradie thank you for the advice i very much appreciate it. Do you have any idea why when the input is 3 and i round it outputs 14.44 instead of 14.45?
8th Mar 2022, 2:27 AM
zack
+ 4
zack , HungryTradie I had a play with your code zack incorporating cei() from the Math module. I have also used cout instead of printf for one of the outputs to show that both options work. I haven't tested against the challenge, so don't know if this is what you need - let me know #include <iostream> #include <iomanip> #include <math.h> using namespace std; int main() { int order; cin >> order; float total = order * 5; float totalAfter; float discount = total * 0.9; if (order > 1) { totalAfter = discount + (discount * 0.07); printf("%.2f", ceil(totalAfter*100)/100); //cout << ceil(totalAfter*100)/100; } else { totalAfter = total + (total * 0.07); //printf("%.2f", ceil(totalAfter*100)/100); cout << ceil(totalAfter*100)/100; } return 0; }
8th Mar 2022, 7:48 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Multiplying by a decimal is a lot like dividing. Dividing causes some conversion errors when base10 converts to binary and back to base10. Often they are of minor magnitude, but this one seems to be causing you grief. There is most probably something like math.round.ceil function that would round up and adjust your output to 2 decimal places, but I don't know it for C++. Add 0.004 to your total. (Give a tradie a hammer and every problem becomes a nail šŸ”ØšŸ’)
8th Mar 2022, 2:53 AM
HungryTradie
HungryTradie - avatar
+ 2
There is probably a correct way to do this.... But +0.004
8th Mar 2022, 1:50 AM
HungryTradie
HungryTradie - avatar
+ 2
zack The code seems to work correctly, are you trying to round up?
8th Mar 2022, 2:05 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
G'day Rik Wittkopp how ya gar'n? It's Kaleidoscope code challenge TestCase1: input 1 gives 5.35 output TestCase2: input 3 gives 14.45 output // Zacks code gave 14.44 Adding 0.005 to the total made test case 4 fail, but adding 0.004 passed all tests. Is there a way to use cout with format specifiers like printf?
8th Mar 2022, 2:18 AM
HungryTradie
HungryTradie - avatar
+ 2
HungryTradie thank you
8th Mar 2022, 2:59 AM
zack
+ 2
Correct Answer - #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int order; cin >> order; float price_per_kaleidoscope = 5.0; float total = order * price_per_kaleidoscope; float discount = 0.0; if (order > 1) { discount = total * 0.1; } float total_discounted = total - discount; float tax = total_discounted * 0.07; float total_after_tax = total_discounted + tax; // Round to two decimal places total_after_tax = round(total_after_tax * 100) / 100; cout << fixed << setprecision(2) << total_after_tax; return 0; }
6th May 2024, 11:32 PM
JaiprakashJH
JaiprakashJH - avatar
+ 1
Hey zack nice code mate. Some other things I reckon you can do to help yourself as a learning programmer: Try to save lines: in this program, you could move any code that is the same outside of the decision. Eg the printf() line is completely the same. Try to use as few variables as you need: in this program, you use total & totalAfter, but total doesn't get used again, so you could just update total. You use discount before your decision, you could move it inside the decision & you could just update total to have the discount value (before you calculate the tax). You could then move the tax calculation outside the decision instead of needing to write that line twice, put it just above the relocated printf(). Hey, this is C++, why are you using printf() instead of cout << Nice work though mate, smash those code coach challenges!
8th Mar 2022, 2:00 AM
HungryTradie
HungryTradie - avatar
+ 1
Rik Wittkopp it works for most of the tests, but on one of them the input is 3 and it comes out as 14.445 or when i attempt to round it comes out 14.44 instead of 14.45
8th Mar 2022, 2:23 AM
zack
+ 1
Rik Wittkopp no its the kaleidoscope coad coach challenge
8th Mar 2022, 2:25 AM
zack
0
zack Is it challenge 17.2 "Who doesn't love a discount"
8th Mar 2022, 2:10 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
/* Using only the basic tools as expected in a Code Coach defined as easy ( for ex. one should not need extra #include statements or methods not yet taught...) : */ #include <iostream> using namespace std; int main() { int unit = 5; float tax = .07; float disc = .1; int sales; cin >> sales; float unitDisc = (unit - unit * disc); float salesDisc = sales * unitDisc + sales * unitDisc * tax; float mult = (salesDisc + .005) * 100; int round = mult ; float out = round ; if (sales == 1) { cout << unit + unit * tax; } else cout << out/100 ; return 0; } Full commented explanations in linked file: https://code.sololearn.com/c11MOzWyGqL0/?ref=app
8th Nov 2022, 5:48 AM
Scott D
Scott D - avatar