Code not working properly, condition is being ignored, no errors.

No matter how hard I try, I can not find anything wrong with this code, lol.


Thanks in advance.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 #include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

 void skaitymas ();
 void poru_skaiciavimas ();
 void atliekamos ();


int i, z, q, c, n,
vyr=0, mot=0, amot=0, avyr=0,
lyt[100],rank[100], dyd[100];

int main(){



skaitymas ();
poru_skaiciavimas ();
atliekamos ();
}


    void skaitymas () {
ifstream in ("a.txt");
in >> n;

    for(i=0; i<n; i++){

    in >> lyt[i] >> rank[i] >> dyd[i];

 // cout << lyt[i] << " " <<rank[i]<<" "<< dyd[i]<<endl; // Reading is working properly as cout outputs needed values so far..
   
 }
    }


        void poru_skaiciavimas () {

        for(z=0; z<n; z++){
            for (q=z+1; q<n-1; q++){

                if ((lyt[z]==3 && lyt[q]==3) && (rank[z]==rank[q]+1 || rank[z]+1==rank[q]) && (dyd[z]==dyd[q])){
                    lyt[z]=0;
                    lyt[q]=0;
                    vyr=vyr+1;
                    cout << "z=" << z << " " << "q=" << q << " " << "vyr=" << vyr << endl; // cout not working anymore;


                     }


                 if ((lyt[z]==4 && lyt[q]==4) && (rank[z]==rank[q]+1 || rank[z]+1==rank[q]) && (dyd[z]==dyd[q])){
                    lyt[z]=0;
                    lyt[q]=0;
                    mot=mot+1; }
            }
        }
}
    void atliekamos (){


    for(c=0; c<n; c++){

        if (lyt[c]==3){avyr=avyr+1;}
        if (lyt[c]==4){amot=amot+1;}
    }

    ofstream out ("b.txt");
    out << mot << endl
    << vyr << endl << amot << endl << avyr;

    }




Last edited on
Are the conditions on line 45 met correctly? if they aren't the code will ignore everything that is between the { } on that if statement.
put a breakpoint on line 30 and tell us what your value for n is?
I figured out dyd[z]==dyd[q] needs to be dyd[z]=dyd[q] as with those 2 conditions code works normally, but the problem remains with rank[z]==rank[q]+1 || rank[z]+1==rank[q], it just doesn't want to do what I want and if I try to delete one equal sign, it gives me
lvalue required as left operand of assignment


So the question emerges, what's the actual difference between dyd[z] = / == dyd[q] ? I thought == means "is equal to" while = is supposed to mean simply "is" when declaring values, isn't it?... and how the heck do i tell it to simply check if one array is equal to other array+1?


put a breakpoint on line 30 and tell us what your value for n is?

Here is the txt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
14
4 1 25
4 1 13
4 2 15
4 2 25
3 2 42
3 2 25
4 1 25
3 1 25
4 1 25
3 1 42
3 1 25
3 1 36
4 1 24
4 1 15


as we can see, 7 and 9 lines lyt[z]=lyt[q]=3, dyd[z]=dyd[q]=25, rank[z]+1=rank[q] (rank[z]=1; rank[q]=2)

so theoretically, conditions are met at least once.

Last edited on
The code as posted in the original post works fine for me with the dataset you posted.

I figured out dyd[z]==dyd[q] needs to be dyd[z]=dyd[q] as with those 2 conditions code works normally

Doubtful. Setting dyd[z] to the value held by dyd[q] means that portion of the condition will always be true except when dyd[q] happens to be 0 (which in this case would be never.) You would also be changing the same element multiple times in the loop, which doesn't seem very useful.
Topic archived. No new replies allowed.