Why codeblocks doesnt print out decimal numbers

I need to get an average of numbers?

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
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>


using namespace std;

int main()
{
    int n;
    int a[500];
    int b[500];
    int viso=0;
    float kai=0;
    float des=0;
    float countDes=0;
    float countKai=0;
    float vidd;
    float vidk;
    float lyg;


    fstream fd("duom.txt");
    ofstream fr("rez.txt");
    cout << "namai" << endl;
    fd >> n;
    for(int i=0; i<n; i++)
    {
        fd >> a[i] >> b[i];
        viso+=b[i];
        lyg=a[i]%2;
        if(lyg==0)
        {
            des+=b[i];
            countDes++;
        }
        else
        {
            kai+=b[i];
            countKai++;
        }



    }
    vidd=des/countDes;
    vidk=kai/countKai;
    fr << viso << is viso gyventoju gatveje << " ";
    fr << 

    fd.close();
    fr.close();

    return 0;
}


vidk and vidd should be printed with a decimal point. In exercise vidk should be printed out as 2.80 but I get 2 and I cant figure out where is the problem


Last edited on
It's called integer division - if the two operands to the / operation are ints then the result will also be an int (truncated toward 0 if necessary).

28 / 10 -> 2

28.0 / 10 -> 2.8

Either cast one of the operands to a double, or make the sums double.

Please show complete code. In code tags.
1
2
fr << viso << is viso gyventoju gatveje << " ";
    fr << 


??? This isn't valid C++ code and won't compile. It also looks like you're modified the code in your first post following comments from lastchance. Please don't. Post a new entry with the revised code so that anyone reading these posts can follow.
Last edited on
Topic archived. No new replies allowed.