For loop welp me

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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include <iostream>
#include <fstream>
using namespace std;
//
const char duom [] = "d.txt";
const char rez [] = "r.txt";
//
//void int EkoVid(int l, int km, int &nr_i, int &vid);
//
int main()
{
    ifstream in(duom);
    ofstream out(rez);
    int n, l, km, nr_i = 0;
    double km_l, best_km_l = 0;
    in >> n;
    for(int i = 0; i <= n; i++){
        in >> l >> km;
        km_l = l * km;
        cout << i << endl;
        if(km_l > best_km_l){
            best_km_l = km_l;
            nr_i = i;
        }
    }
    cout << "Geriausias pylimas buvo: " << nr_i << endl;
    in.close();
    out.close();
    return 0;
}
Last edited on
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.

PPS: You never use your out stream.
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
using namespace std;
//
const char duom [] = "d.txt";
const char rez [] = "r.txt";
//
//void int EkoVid(int l, int km, int &nr_i, int &vid);
//
int main()
{
    ifstream in(duom);
    ofstream out(rez);
    int n, l, km, nr_i = 0;
    double km_l, best_km_l = 9999;
    in >> n;
    for(int i = 1; i <= n; i++){
        in >> l >> km;
        km_l = l / km;
        if(km_l < best_km_l){
            best_km_l = km_l;
            nr_i = i;
        }
    }
    cout << "Geriausias pylimas buvo: " << nr_i << endl;
    return 0;
}


I tried to make best_km_l = 9999 so that km_l would be already lower than and would assign it to best_km_l.
Last edited on
Okay so I tried
cout << km_l << endl;
and all 5 times I get 0. What could be wrong ?
and all 5 times I get 0. What could be wrong ?

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;
Last edited on
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.
Topic archived. No new replies allowed.