I have a text file:
5
7 100
6 112
12 160
5 80
9 150
n is number how many times a guy went to fill gas.
The first number is fuel (l) how much he filled.
And the second is km.
So I did km_l = l / km
and I was supposed to assign i to the best time he went to fill the car.
The problem with the code is that can't get nowhere close to answer.
(Ignore the void)
n = 5.
Your for loop is looping from i = 0 to i = 5, inclusive. This is a total of 6 times (0, 1, 2, 3, 4, 5).
If you want the first item to be printed as "1", then do for (int i = 1; i <= n; i++)
If you want the first item to be printed as "0", then do for (int i = 0; i < n; i++)
(or you can just change it to be the latter, but do nr_i = i + 1; to change a 0 to a 1.)
PS: Please use better variable names. "l" (even if in a different language) is a bad variable name. Also, C++ streams are closed automatically at the end of the stream object's scope, you can remove lines 27 and 28.
Yeah, I realized one thing that it's actually not an i problem but it doesn't calculate correctly probably if statement.
I'm not using out because I want first to check my answer faster in the cmd so cout is way better to use atm.
Now you're doing division instead of multiplication.
Division between two integers is called "integer division", which means it truncates the decimal-place digits off:
(5/3 == 1, not 1.667).
You need to cast one of the operands to a double in order to do "proper" floating-point division. km_l = static_cast<double>(l) / km;
Okay thank you very much for the help and explaining about the "integer division" I actually had no clue that it does that. I thought having double km_l at the start was enough.