Void function refuses to work

Despite all the mess you gonna see in there I need somehow to make my void function to work and not give me error :
"C:\Users\dulin\Desktop\New folder\darbas.cpp|19|error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'|"

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
using namespace std;
//
const char duom [] = "d.txt";
const char rez [] = "r.txt";
//
void EcoVid(int km, int l, int &nr_i, int &vid, int n);
//
int main()
{
    ifstream in(duom);
    ofstream out(rez);
    int n, l, km, nr_i;
    double km_litrai, best_km_litrai, vid;
    in >> n;
    for(int i = 1; i <= n; i++){
        in >> l >> km;
        EcoVid(km, l, nr_i, vid, n);
        out << "Geriausias pylimas buvo: " << nr_i << endl;
        out << "Vidutines kuro sanaudos: " << vid << endl;
    }
    return 0;
}
//
void EcoVid(int km, int l, int &nr_i, int &vid, int n){
    double km_litrai, best_km_litrai = 9999;
    nr_i = 0;
    vid = 0;
    kmsum = 0;
    lsum = 0;
    for(int i = 1; i <= n; i++){
        km_litrai = (double)l / km;
        if(km_litrai < best_km_litrai){
            best_km_litrai = km_litrai;
            nr_i = i;
        }
        kmsum += km;
        lsum += l;
        vid = (double)lsum / kmsum;
    }

}
If that's the error you're seeing then you've posted the wrong code. In the code you've posted you are passing a double (vid) to an int&. And you refer to a couple of variables that don't exist (kmsum and lsum).
Updated. I still receive the same error.
EDIT : Okay I changed it to double &vid

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
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
using namespace std;
//
const char duom [] = "d.txt";
const char rez [] = "r.txt";
//
void EcoVid(int km, int l, int &nr_i, int &vid, int n);
//
int main()
{
    ifstream in(duom);
    ofstream out(rez);
    int n, l, km, nr_i;
    double vid;
    in >> n;
    for(int i = 1; i <= n; i++){
        in >> l >> km;
        EcoVid(km, l, nr_i, vid, n);
        out << "Geriausias pylimas buvo: " << nr_i << endl;
        out << "Vidutines kuro sanaudos: " << vid << endl;
    }
    return 0;
}
//
void EcoVid(int km, int l, int &nr_i, int &vid, int n){
    double km_litrai, best_km_litrai = 9999;
    int kmsum = 0, lsum = 0;
    nr_i = 0;
    vid = 0;
    for(int i = 1; i <= n; i++){
        km_litrai = (double)l / km;
        if(km_litrai < best_km_litrai){
            best_km_litrai = km_litrai;
            nr_i = i;
        }
        kmsum += km;
        lsum += l;
        vid = (double)lsum / kmsum;
    }
}
Last edited on
Actually the type of the parameter vid in EcoVid(...) is int while you provide a double in main (line 19). I would think that the parameter for EcoVid(...) is supposed to be a double.
Topic archived. No new replies allowed.