Can't figure out where I'm off

First... My 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

//This program teraforms a map based on input from a "History of Disasters" file which is the history of what has happened to land through time given a begining map and provides the end map and all maps in between all disasters. 

void plot_map(const vector<vector<char> > &someMap, ostream & outstream); //Outputs the map
void HorizontalQuake(int row, int col, vector<vector<char> > & map); //replaces an entire row with '-'
void VerticalQuake(int row, int col, vector<vector<char> > & map); //replaces an entire column with '|'
void HorizontalChasm(int row, int col, vector<vector<char> > & map); //erases an entire row
void VerticalChasm(int row, int col, vector<vector<char> > & map); //erases an entire column
void Meteor(int row, int col, int reach, vector<vector<char> > & map, int rowC, int colC, vector<vector<vector<char> > > & mapset); //replaces an area affected by meteor by Ms
vector<vector<vector<char> > > mapset;


void plot_map(const vector<vector<char> > &someMap, ostream & outstream)
{
	int a = mapset.size();
	for(a; a > 0; a--)
	{
		int b = mapset[a].size(); //number of rows for outputted map
		for(int c = 0; b > c; c++)
		{
			int d = mapset[a][c].size();
			for(int e = 0; d > e; e++)
			{
				outstream << mapset[a][c][e];
			}
		}
		outstream << endl;
	}
	return;
}
void HorizontalQuake (int row, int col, vector<vector<char> > & map)
{
	int colI = col;
	for(colI; colI > 0; colI--)
	{
		map[row][colI] = '-';
	}
	return;
}
void HorizontalChasm (int row, int col, vector<vector<char> > & map)
{
	map.erase(map.begin()+row+1);
	return;
}
void VerticalQuake(int row, int col, vector<vector<char> > & map)
{
	int rowI = row;
	for(rowI; rowI > 0; rowI--)
	{
		map[row][col] = '|';
	}
	return;
}
void VerticalChasm(int row, int col, vector<vector<char> > & map)
{
	int rowI;
	rowI = row;
	for(rowI; rowI > 0; rowI--)
	{
		map[rowI].erase(map[rowI].begin() + col);
	}
	return;
}
void Meteor(int row, int col, int reach, vector<vector<char> > & map, int rowC, int colC, vector<vector<vector<char> > > & mapset)
{
	int rowI = rowC;
	int colI = colC;
	if(row < 1 or col < 1 or row > rowC or col > colC)
	{
		return;
	}
	else
	{
		for(rowI; rowI > 0; rowI--)
		{
			for(colI; colI > 0; colI--)
			{
				if((rowC-reach) < rowI and (rowC+reach) > rowI and (colC-reach) < colI and (colC+reach) > colI)
				{
					map[rowI][colI] = 'M';
					mapset.push_back(map);
				}
			}
		}
	}
	return;
}



int main()
{
	int rowO, colO, rowC, colC, rowI, colI; //row and column orginial, current, and index
	string dis; //string of disaster
	ifstream mapfile; //the map which is given
	ifstream hond; //history_of_natural_disasters
	mapfile.open("map.txt");
	hond.open("history_of_natural_disasters.txt");
	mapfile >> rowO >> colO;
	vector<char> rows(rowO);
	vector<vector<char> > map(colO, rows);
	rowC = rowO;
	colC = colO;
	rowI = rowO;
	colI = colO;
	//following for loop records the current map
	char temp;
	for(rowI; rowI > 0; rowI--)
	{
		for(colI; colI > 0; colI--)
		{
			mapfile >> temp;
			map[(rowC - rowI)][(colC-colI)] = temp;
		}
	}
	hond >> dis;
	int location, loc1, loc2, reach;
	mapset.push_back(map);
	while(!hond.eof())
	{
		if(dis == "HorizontalQuake")
		{
			hond >> location;
			if(location <= rowC and location > 0)
			{
				HorizontalQuake(location, colC, map);
				mapset.push_back(map);
			}
		}
		else if(dis == "HorizontalChasm")
		{
			hond >> location;
			if(location <= rowC and location > 0)
			{
				HorizontalChasm(location, colC, map);
				mapset.push_back(map);
				rowC--;
			}
		}
		else if(dis == "VerticalQuake")
		{
			hond >> location;
			if(location <= colC and location > 0)
			{
				VerticalQuake(rowC, location, map);
				mapset.push_back(map);
			}
		}
		else if(dis == "VerticalChasm")
		{
			hond >> location;
			if(location <= rowC and location > 0)
			{
				VerticalChasm(rowC, location, map);
				mapset.push_back(map);
				colC--;
			}
		}
		else if(dis == "Meteor")
		{
			hond >> loc1 >> loc2 >> reach;
			Meteor(loc1, loc2, reach, map, rowC, colC, mapset);
			//loc1 = row number, loc2 = column number
			//further note to self, the boundry check is done within the function
		}
		else
		{
			cout << "The code failed, and I am frustrated! Screw you Engr101" << endl;
			return 0;
		}
		hond >> dis;
	}
	ofstream map_history;
	map_history.open("map_history.txt");
	plot_map(map, map_history);
	map_history.close();
	return 0;
}


When I comment out everything that involves a for loop, I have memory allocation errors (someone helped me with that here earlier, but they're still happening even though I made necessary changes. I looked through and couldn't find another source).

When I don't have any comment outs, I have a segmentation fault after it runs through vertical chasm 24 times.

Any ideas? Thank you.
You don't do any explicit allocations. How can you get memory leaks?

What's more likely, us that your vector indices may be out of range. use .at rather than [] to check for range errors.
Okay... Now I'm getting seg faults when I changed all the [] to .at... I changed some other issues (two things I changed now went from looking right to me to looking wrong or unnecessary but atleast it's working...) but it's still seg faulting.

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

//This program teraforms a map based on input from a "History of Disasters" file which is the history of what has happened to land through time given a begining map and provides the end map and all maps in between all disasters. 

void plot_map(const vector<vector<char> > &someMap, ostream & outstream); //Outputs the map
void HorizontalQuake(int row, int col, vector<vector<char> > & map); //replaces an entire row with '-'
void VerticalQuake(int row, int col, vector<vector<char> > & map); //replaces an entire column with '|'
void HorizontalChasm(int row, int col, vector<vector<char> > & map); //erases an entire row
void VerticalChasm(int row, int col, vector<vector<char> > & map); //erases an entire column
void Meteor(int row, int col, int reach, vector<vector<char> > & map, int rowC, int colC, vector<vector<vector<char> > > & mapset); //replaces an area affected by meteor by Ms
vector<vector<vector<char> > > mapset;


void plot_map(const vector<vector<char> > &someMap, ostream & outstream)
{
	int a = mapset.size();
	for(a; a > 0; a--)
	{
		int b = mapset.at(a).size(); //number of rows for outputted map
		for(int c = 0; b > c; c++)
		{
			int d = mapset.at(a).at(c).size();
			for(int e = 0; d > e; e++)
			{
				outstream << mapset.at(a).at(c).at(e);
			}
		}
		outstream << endl;
	}
	return;
}
void HorizontalQuake (int row, int col, vector<vector<char> > & map)
{
	int colI = col;
	for(colI; colI > 0; colI--)
	{
		map.at(row).at(colI) = '-';
	}
	return;
}
void HorizontalChasm (int row, int col, vector<vector<char> > & map)
{
	map.erase(map.begin()+row+1);
	return;
}
void VerticalQuake(int row, int col, vector<vector<char> > & map)
{
	int rowI = row;
	for(rowI; rowI > 0; rowI--)
	{
		map.at(row).at(col) = '|';
	}
	return;
}
void VerticalChasm(int row, int col, vector<vector<char> > & map)
{
	int rowI;
	rowI = row;
	for(rowI; rowI > 0; rowI--)
	{
		map.at(rowI).erase(map.at(rowI).begin() + col);
	}
	return;
}
void Meteor(int row, int col, int reach, vector<vector<char> > & map, int rowC, int colC, vector<vector<vector<char> > > & mapset)
{
	int rowI = rowC;
	int colI = colC;
	if(row < 1 or col < 1 or row > rowC or col > colC)
	{
		return;
	}
	else
	{
		for(rowI; rowI > 0; rowI--)
		{
			for(colI; colI > 0; colI--)
			{
				if((rowC-reach) < rowI and (rowC+reach) > rowI and (colC-reach) < colI and (colC+reach) > colI)
				{
					map.at(rowI).at(colI) = 'M';
					mapset.push_back(map);
				}
			}
		}
	}
	return;
}



int main()
{
	int rowO, colO, rowC, colC, rowI, colI; //row and column orginial, current, and index
	char a;
	string dis; //string of disaster
	ifstream mapfile; //the map which is given
	ifstream hond; //history_of_natural_disasters
	mapfile.open("map.txt");
	hond.open("history_of_natural_disasters.txt");
	mapfile >> rowO;
	mapfile >> a;
	mapfile >> colO;
	vector<char> rows(rowO);
	vector<vector<char> > map(colO, rows);
	rowC = rowO;
	colC = colO;
	rowI = rowO;
	colI = colO;
	//following for loop records the current map
	char temp;
	for(colI; colI > 0; colI--)
	{
		for(rowI; rowI > 0; rowI--)
		{
			mapfile >> temp;
			map.at((rowC - rowI)).at((colC-colI)) = temp;
		}
	}
	hond >> dis;
	int location, loc1, loc2, reach;
	mapset.push_back(map);
	while(!hond.eof())
	{
		if(dis == "HorizontalQuake")
		{
			hond >> location;
			if(location <= rowC and location > 0)
			{
				HorizontalQuake(location, colC, map);
				mapset.push_back(map);
			}
		}
		else if(dis == "HorizontalChasm")
		{
			hond >> location;
			if(location <= rowC and location > 0)
			{
				HorizontalChasm(location, colC, map);
				mapset.push_back(map);
				rowC--;
			}
		}
		else if(dis == "VerticalQuake")
		{
			hond >> location;
			if(location <= colC and location > 0)
			{
				VerticalQuake(rowC, location, map);
				mapset.push_back(map);
			}
		}
		else if(dis == "VerticalChasm")
		{
			hond >> location;
			if(location <= rowC and location > 0)
			{
				VerticalChasm(rowC, location, map);
				mapset.push_back(map);
				colC--;
			}
		}
		else if(dis == "Meteor")
		{
			hond >> loc1 >> loc2 >> reach;
			Meteor(loc1, loc2, reach, map, rowC, colC, mapset);
			//loc1 = row number, loc2 = column number
			//further note to self, the boundry check is done within the function
		}
		else
		{
			cout << "The code failed, and I am frustrated! Screw you Engr101" << endl;
			return 0;
		}
		hond >> dis;
	}
	ofstream map_history;
	map_history.open("map_history.txt");
	plot_map(map, map_history);
	map_history.close();
	return 0;
}
Last edited on
I dont know how the code is evening compiling, never seen a or & and operator in C++ unless they are typedef'd somewhere.
how can you do this:
1
2
vector<char> rows(rowO);
vector<vector<char> > map(colO, rows); 

do something like this:
1
2
vector<char> rows;
rows.push_back(rowO);

same for the maps. and first initialize the values.
Last edited on
There is a header file, that comes with C++ which is part of the CLibrary that does just those defines
http://www.cplusplus.com/reference/clibrary/ciso646/

writetonsharma,

1
2
vector<char> rows(rowO);
vector<vector<char> > map(colO, rows);


creates a vector of rowO length. Yours creates a vector and puts the value of rowO in it.

Doing further testing, when I'm using only the horizontalchasm function, I get an infinite loop from that while loop in int main. I don't know why, but it's like horizontalchasm is never returning or something.
Last edited on
I apologize for not seeing it correctly. I thought you are trying to insert the values in the vector.
Can you paste your map.txt file?
Last edited on
Topic archived. No new replies allowed.