List Iterator Not Working

Note: Using Code::Blocks on Windows.

I am trying to get some (x, y) coords from a .txt file, sort them by their distance from the main (x, y) coord, and then save them to another .txt file.

I put the coords from the file in a list<pair<int, pair<int, int> > >. The first int is the distance from the main coord. The other two ints are the coords from the file.

Once I get these things into the list and the distances calculated (using a^2 + b^2 = c^2. I leave it in c^2 form), I try to see what I have so far in the unsorted list by printing it all out. Like so:

1
2
3
4
for(it = farms.begin(); it != farms.end(); it++);
{
    cout << (*it).first << ' ' << (*it).second.first << '|' << (*it).second.second << endl;
}


However it only prints one thing out that is not even anything in the .txt file.

What is going on, and how can I fix it?

Full Code.

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <utility>
#include <string>
#include <list>

using namespace std;

int main(int argc, char* args[])
{
    list<pair<int, pair<int, int> > > farms;
    list<pair<int, pair<int, int> > >::iterator it, it2;

    pair<int, int> origin = make_pair(691, 299);
    string in_filename = "farms.txt";
    string out_filemane = "farms_sorted.txt";

    ifstream in_file(in_filename.c_str(), ios::in);

    if(!in_file.good())
    {
        cout << "Error while opening file!";
        return 1;
    }

    while(in_file.good())
    {
        char n = in_file.get();
        char temp_1[4] = "";
        int temp_2 = 0;
        int temp_3 = 0;
        int x = 0;
        int y = 0;

        while(in_file.good() && n != '(')
        {
            in_file.get(n);
        }

        if(!in_file.good()) break;

        in_file.get(temp_1, 4, '|');
        x = atoi(temp_1);

        // Moving the get pointer ahead one.
        in_file.get();

        in_file.get(temp_1, 4, ')');
        y = atoi(temp_1);

        temp_2 = origin.first - x;
        temp_3 = origin.second - y;

        temp_2 *= temp_2;
        temp_3 *= temp_3;

        temp_2 = temp_2 + temp_3;

        farms.push_back(make_pair(temp_2, make_pair(x, y)));

        cout << temp_2 << " " << x << '|' << y << endl;
    }

    cout << endl;

    for(it = farms.begin(); it != farms.end(); it++);
    {
        cout << (*it).first << ' ' << (*it).second.first << '|' << (*it).second.second << endl;
    }

    return 0;
}
I suggest you to limit the scope of your variables
1
2
3
4
5
    for(it = farms.begin(); it != farms.end(); it++); //semicolon, that's an statement
    {
    //this is outside the loop, now 'it' is invalid
        cout << (*it).first << ' ' << (*it).second.first << '|' << (*it).second.second << endl;
    }
Wow all this trouble over a semicolon... Thank you for pointing that out... I feel so dumb now.

Well this is solved. I just needed an extra pair of eyes.

Again thank you.
Topic archived. No new replies allowed.