+ 1

Kaleidoscope challenge to many numbers after decimal

I was having a bug with this code on test 2 the answer is xx.xx but the output i get is xx.xxx #include <iostream> using namespace std; int main() { int buy; cin>>buy; double kaleidoscope=5; double saleP=buy*kaleidoscope; if (buy>=2){ saleP =saleP -saleP*.1; cout<<saleP+.07*saleP;} else cout<<saleP+.07*saleP; return 0; }

25th Nov 2021, 9:58 PM
Angel Jimenez
Angel Jimenez - avatar
3 Réponses
+ 4
Angel Jimenez , there is also a possible solution by using printf() function with the format specifier: "%.2f". this will set the output to 2 decimal places. so we can pass the test cases. but there is an other point, that should be solved. there is some duplication by calculating the prices and by doing the output. instead of using: cout << saleP + 0.07 * saleP; we can do: printf("%.2f", saleP + 0.07 * saleP);
26th Nov 2021, 5:46 PM
Lothar
Lothar - avatar
26th Nov 2021, 12:26 AM
Ipang
+ 1
Ok thank you i dont think ive learned printf yet
26th Nov 2021, 6:16 PM
Angel Jimenez
Angel Jimenez - avatar