Why Integer with Double Digits is Not Looping Out

closed account (9h5X216C)
Solved. Codes removed to protect assignment copying.
Last edited on
I think your x and y are backwards here:

 
grid[y][x]  = cityid;

Try putting the x first.
closed account (9h5X216C)
@tpd

I tried switching it over but it still has a problem. Managed to solve it by removing while(!file.eof).

Thank you so much!
I hate it when people erase stuff.
The code looked something like this, although I fixed it up a little (remove globals, for instance).

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const int gridX = 8; 
const int gridY = 10;

struct Data {
    int X, Y;
    string Id, Name;

    Data(int x, int y, string id, string name)
        : X(x), Y(y), Id(id), Name(name) {}
};

void HeaderFooter(int Ycols) {
    cout << "   #";      
    for (int c=0; c<Ycols+1; ++c)
        cout << " #";
    cout << '\n';
}

void ShowGridMap(vector<vector<string>>& grid) {
    int col = grid[0].size();

    HeaderFooter(col);

    for (auto it = grid.crbegin(); it != grid.crend(); ++it) {
        cout << setw(2) << gridY-(it - grid.crbegin()) << " #";
    for (auto it_2 = it->cbegin(); it_2 != it->cend(); ++it_2)
        cout << ' ' << *it_2;
        cout << " #" << endl;
    }

    HeaderFooter(col);

    cout << "    ";
    for (int c=0; c<col; ++c)
        cout << ' ' << c;
    cout << "\n\n";
}

void displayCityMap(vector<Data> &data) {
    vector<string> cityloData;

    fstream file("TestCases_CityLocation.txt");
    if (!file.is_open ()) {
            cerr << "Error opening file:\n";// << file << ", Aborting program." << endl;
        cout << "Press <enter> to go back to main menu ..." << endl;
        cin.get();
        cin.ignore();
    }

    string line;
    char delimiters [] = "[],-";    //to take away characters in file
    while (getline(file, line)) {
        if (line.length() != 0 ) {
            for(int i = 0; delimiters[i]; ++i) {
                replace(line.begin(), line.end(), '-', ' ');
                line.erase(remove(line.begin(), line.end(), delimiters[i]),
                           line.end());
            }
            if(line.size() > 0) {
                cityloData.push_back(line);
            }
        }       
    }
    file.close();

    for(int j=0; j < (int)cityloData.size(); j++) {
        string str = cityloData[j];
        string tempX, tempY, tempId, tempName;

        istringstream iss(str);
        iss >> tempX >> tempY >> tempId >> tempName;

        int rowsX, colsY;
        stringstream xx(tempX);
        stringstream yy(tempY);

        xx >> rowsX;
        yy >> colsY;

        data.push_back(Data(rowsX, colsY, tempId, tempName));

        /*
        cout <<  "X:"  << setw(2) << data[j].X
             << " Y:"  << setw(2) << data[j].Y
             << " ID:" << data[j].Id
             << " City Name:" << data[j].Name << endl;
        */
    }
}

void displayGrid() {
    vector<Data> data;

    displayCityMap(data);

    int Xrows = gridX+1;
    int Ycols = gridY+1;

    cout << "X " << Xrows << " Y " << Ycols << endl;

    vector<vector<string>> grid(Xrows, vector<string>(Ycols, " "));

    cout << "X " << Xrows << endl;

    for(int i = 0; i < (int)data.size(); i++) {
        int x = data.at(i).X;
        int y = data.at(i).Y;
        string cityid = data.at(i).Id;
        cout << y << endl;
        grid[x][y]  = cityid;
    }
    ShowGridMap(grid);
}

int main() {
    displayGrid();
}

Codes removed.
DON'T do that. It makes the thread useless as a resource for others. It's selfish of you.
closed account (E0p9LyTq)
It's selfish of you.

Really selfish since the OP openly admitted they were doing this for homework.
Topic archived. No new replies allowed.