+ 1

Want to know how and which methods i can use to make this code better

finished the code but it is very messy and wondered what i can do to make it better (without changing its function ) https://sololearn.com/compiler-playground/c0A8333s6IhX/?ref=app as long as function of code doesnt change i am open for any advices

21st May 2024, 2:47 PM
learner
learner - avatar
4 Answers
+ 4
you can use if inside if. I see the x has values 1,2,3 so you can remove all checks with x and do general checks like this if (x == 1) { //add all other checks had x==1 here } else if (x == 2) { //add all checks had x==2 } else { //add all rest } Other way is to use switch-case but for me if is only 3 cases i prefer use if-else. Also you not have to print inside every case. Just save the value and at the end of your program print 1 message with your value. If do that you have just 1 line code inside each case you you can ingore the branches. Like seem like this: ``` if (...) value = 1; else if (...) value = 2; else if (...) value = 3; ... cout << "Cost for a nigh is" << value; ```
21st May 2024, 9:12 PM
john ds
john ds - avatar
+ 2
Potentially use functions so that there is more structure to the program and your main function looks cleaner. Maybe a calculatePricePerNight function for example.
21st May 2024, 3:23 PM
Kai Harrison
22nd May 2024, 9:32 AM
Bob_Li
Bob_Li - avatar
0
First you can use clearer variable names (roomType, floor, nights, pricePerNight, totalPrice, finalPrice) and explain the input more in the beginning process. Next you can structure the price-calculation and the discount-calculation a little better, so it is determined more efficiently and reads clearer and more precise. You can use some constants (discountThreshold, discountRate) for better readability and adjustability. Also maybe you could manage error messages somehow diferent and provides specific error messages for invalid inputs. Just some ideas though, keep on coding! 👍
22nd May 2024, 11:46 PM
Konan
Konan - avatar