+ 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
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;
```
+ 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.
0
learner
you can define a function. as Kai Harrison suggested.
https://sololearn.com/compiler-playground/cU4Kv9SG8MqF/?ref=app
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! đ