+ 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; }
3 Antworten
+ 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);
+ 1
Take a look at these I/O manipulators from <iomanip> header
https://en.cppreference.com/w/cpp/io/manip/setprecision
https://en.cppreference.com/w/cpp/io/manip/fixed
+ 1
Ok thank you i dont think ive learned printf yet