Name lookup of 'i' changed for ISO

I need to program an action, that would find the lowest common multiple (LCM) of 18 (j) and 7 (i), but it it shows an error. What is wrong here?

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
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
void Skaityti (int & n, int V[]);
void Spausdinti (int n, int V[]);
int main()
{
setlocale (LC_ALL, "Lithuanian");
int n;
int V[100];
Skaityti (n, V);
Spausdinti (n, V);
return 0;
}
void Skaityti (int & n, int V[])
{
    ifstream fd ("varles.txt");
    fd >> n;
    for (int i=0; i < n; i++)
    {
    }
}
void Spausdinti (int n, int V[])
{
int p;
int t;
int h; 
int m; 
int k=1; 
int k1=1;
for (int j=0; j < n-1; j++)
{
    for (int i=j+1; i < n; i++)
    if (V[j] > V[i])
    {
        V[i] += k1++;
    }
       if (V[i] != V[j])
       {
            V[j] += k++;
       }
}

cout << k << " " << k1;
}
A little better indentation might help you see the problem.

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
#include <iomanip>
#include <fstream>
#include <iostream>

using namespace std;

void Skaityti (int & n, int V[]);
void Spausdinti (int n, int V[]);

int main()
{
    setlocale (LC_ALL, "Lithuanian");
    int n;
    int V[100];
    Skaityti (n, V);
    Spausdinti (n, V);
    return 0;
}

void Skaityti (int & n, int V[])
{
    ifstream fd ("varles.txt");
    fd >> n;
    for (int i = 0; i < n; i++)
    {
    }
}

void Spausdinti (int n, int V[])
{
    int p;
    int t;
    int h;
    int m;
    int k = 1;
    int k1 = 1;
    for (int j = 0; j < n - 1; j++)
    {
        for (int i = j + 1; i < n; i++)  // See note below.
            if (V[j] > V[i])
            {
                V[i] += k1++;
            }
        if (V[i] != V[j])  // This statement is not part of the for() statement directly above so i is not defined.
        {
            V[j] += k++;
        }
    }

    cout << k << " " << k1;
}


Note: Remember that without braces only one line is included in the body of a control statement.
Your indentation is not the most informative. Lets try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for ( int j=0; j < n-1; j++ )
{
    for ( int i=j+1; i < n; i++ )
    {
        if (V[j] > V[i])
        {
            V[i] += k1++;
        }
    } // scope of i ends here. Been so for years.

    if (V[i] != V[j])
    {
        V[j] += k++;
    }
}
1
2
3
4
5
6
7
8
9
    for (int i=j+1; i < n; i++)
    if (V[j] > V[i])
    {
        V[i] += k1++;
    }
       if (V[i] != V[j])
       {
            V[j] += k++;
       }


I'm not sure what's going on with that indentation, but I believe it's confusing you as well as me.
Your for loop (line 34) has no { curly braces } after it, so only the if-statement on line 35 is within the for loop.

Perhaps you meant something like this?
(not tested)
1
2
3
4
5
6
7
8
9
10
11
    for (int i=j+1; i < n; i++)
    {
        if (V[j] > V[i])
        {
            V[i] += k1++;
        }
        if (V[i] != V[j])
        {
             V[j] += k++;
        }
    }
Yes, it works now. Thank you guys so much!
Topic archived. No new replies allowed.