0

Why is it not giving the correct answer??

#include <iostream> using namespace std; int main() { int klaidoscopes; cin>>klaidoscopes ; float tax=(5*7)/100; float discount=(5*10)/100; int purchase=klaidoscopes*5; float total=purchase+tax; float d_total=total-discount; if(klaidoscopes >1){ cout<<d_total; } else{ if(klaidoscopes ==1){ cout<<total; } } return 0; } You sell souvenir kaleidoscopes at a gift shop, and if a customer buys more than one, they get a 10% discount on all of them! Given the total number of kaleidoscopes that a customer buys, let them know what their total will be. Tax is 7%. All of your kaleidoscopes cost the same amount, 5.00. Task: Take the number of kaleidoscopes that a customer buys and output their total cost including tax and any discounts. Input Format: An integer value that represents the number of kaleidoscopes that a customer orders. Output Format: A number that represents the total purchase price to two decimal places. Sample Input: 4 Sample Output: 19.26

2nd Jul 2024, 5:05 AM
Haram Abbas Lar
Haram Abbas Lar - avatar
7 ответов
+ 3
tax is added to the base price, so it's 1 + discount is subtracted from the base price, so it's 1 - These are multipliers, not the actual value. So you don't just subtract one from the other. use 1.0 instead of 1 to ensure float values are not truncated. since the challenge requires two decimal precision, I used cmath round and *100/100.
8th Jul 2024, 10:07 AM
Bob_Li
Bob_Li - avatar
+ 2
There are calculation errors. I suggest you put it into playground and run it with the sample input, and see if it gets the sample output. If they are not the same, then reduce the input number by 1 at each time, and see if there is any pattern.
2nd Jul 2024, 5:45 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
It's not recommended to give answers directly, but since it's complicated to explain... #include <iostream> #include <cmath> // for round using namespace std; int main(){ int klaidoscopes; cin >> klaidoscopes; int purchase = klaidoscopes * 5; float tax = 1.0 + 7.0/100; //7% tax float discount = 1.0 - 10.0/100; //10% discount float total = purchase * tax; float d_total = round(total*discount*100)/100;//2 decimal places if(klaidoscopes >1) cout<<d_total; else cout<<total; }
8th Jul 2024, 9:36 AM
Bob_Li
Bob_Li - avatar
+ 2
The main problem lies in: float total = purchase + tax; The way you calculate total cost before discount is incorrect. It only taxed one piece of kaleidoscopes, same goes for discount. Also, an integer division always yields an integer float discount=(5*10)/100; You can divide it by 100.0 (a float), or type casting it: (float) 100
8th Jul 2024, 10:25 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Haram Abbas Lar Wong Hei Ming is right, the way you are calculating the values is wrong. That aside, you are also not rounding your float to 2 decimal places. two ways: cmath round(f*100)/100 std::setprecision(2)
2nd Jul 2024, 5:56 AM
Bob_Li
Bob_Li - avatar
+ 1
Thank you
8th Jul 2024, 12:31 PM
Haram Abbas Lar
Haram Abbas Lar - avatar
0
I don't understand
8th Jul 2024, 6:37 AM
Haram Abbas Lar
Haram Abbas Lar - avatar