#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int a;
double b;
cout << "Enter Distance: ";
cin >> a;
if (a <= 100)
{
b = a * 5;
}
elseif (a > 100)
{
a = a - 100;
b = (5 * 100) + (a * 8);
}
elseif (a > 500)
{
a = a - 500;
b = (5 * 100) + (8 * 400) + (a * 10);
}
elseif (a > 1000)
{
a = a - 1000;
b = (5 * 100) + (8 * 400) + (10 * 500) + (a * 12);
}
cout << "Cost = $" << fixed << setprecision(2) << b << endl;
cout << endl;
system("pause");
return 0;
}
Now, I am getting accurate outcomes for numbers within 500.. but when I enter a number above 500, I don't get an accurate answer, and I don't know why.. This is what I am getting when I enter 501
https://imgur.com/a/J6wc326
( Enter Distance: 501
Cost: $3708.00 )
It should have been 3710 but I don't know why is it showing 3708...
Similarly, when I entered 1001
https://imgur.com/a/g4QEZPP
(Enter Distance: 1001
Cost: $7708.00 )
This again is wrong answer and far from accurate one...
However, my answer is always accurate if it is within 500 range... Can someone tell me what's the problem with my code? :/ Thanks a lot!
Oh damn! Sorry for posting this question, this was such a basic error and I spent around 60-120 mins and still couldn't figure it out, wow! :/
Thanks a lot though for pointing it out. :| I just need to use && function there :(
what he said and what you're saying is literally the same thing but in a different order -_-
Similar, yes but not identical. I prefer to stick with the simpler "single" comparison operators (<, >, ==) over the slightly more complex compound operators. IMO, that makes the logic clearer.
Besides I did say "just reverse the if/else if chain", not change the logic of the program.