Out of range at memory location

I have to read in two names from a text file and change each letter to its corresponding number value and multiply all the numbers in the string. If that number mod 47 is equal to the second word mod 47, then I print GO to the file, else it prints STAY.

I keep getting an out of range error.

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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main()
{
	//char comet[6];
	//char group[6];
	string comet;
	string group;
	ifstream in("ride.txt");
	ofstream out("rideo.txt");

	in >> comet;
	in >> group;

	in.close();

	int a = comet.size();

	for(int i =0;i<=a;i++)
	{
		char letter = group.at(i);

		switch(letter)
		{
		case 'A': 
			comet.replace(i, 1, "1");
			break;
		case 'B':
			comet.replace(i, 1, "2") ;
			break;
		case 'C': 
			comet.replace(i, 1, "3") ;
			break;
		case 'D':
			comet.replace(i, 1, "4") ;
			break;
		case 'E': 
			comet.replace(i, 1, "5") ;
			break;
		case 'F':
			comet.replace(i, 1, "6") ;
			break;
		case 'G': 
			comet.replace(i, 1, "7") ;
			break;
		case 'H':
			comet.replace(i, 1, "8");
			break;
		case 'I': 
			comet.replace(i, 1, "9") ;
			break;
		case 'J':
			comet.replace(i, 1, "10") ;
			break;
		case 'K': 
			comet.replace(i, 1, "11") ;
			break;
		case 'L':
			comet.replace(i, 1, "12") ;
			break;
		case 'M': 
			comet.replace(i, 1, "13") ;
			break;
		case 'N':
			comet.replace(i, 1, "14") ;
			break;
		case 'O': 
			comet.replace(i, 1, "15") ;
			break;
		case 'P':
			comet.replace(i, 1, "16");
			break;
		case 'Q': 
			comet.replace(i, 1, "17") ;
			break;
		case 'R':
			comet.replace(i, 1, "18") ;
			break;
		case 'S': 
			comet.replace(i, 1, "19") ;
			break;
		case 'T':
			comet.replace(i, 1, "20") ;
			break;
		case 'U': 
			comet.replace(i, 1, "21") ;
			break;
		case 'V':
			comet.replace(i, 1, "22") ;
			break;
		case 'W': 
			comet.replace(i, 1, "23") ;
			break;
		case 'X':
			comet.replace(i, 1, "24") ;
			break;
		case 'Y': 
			comet.replace(i, 1, "25");
			break;
		case 'Z':
			comet.replace(i, 1, "26");
			break;
		}
	}

	int multComet =1;

	int b = comet.size();

	for(int i =0;i<=b;i++)
	{
		multComet *= comet.at(i);
	}

	int c = group.size();


	for(int i =0;i<=c;i++)
	{
		char letter = group.at(i);
		switch(letter)
		{
		case 'A': 
			group.at(i) = 1;
			break;
		case 'B':
			group.at(i) = 2;
			break;
		case 'C': 
			group.at(i) = 3;
			break;
		case 'D':
			group.at(i) = 4;
			break;
		case 'E': 
			group.at(i) = 5;
			break;
		case 'F':
			group.at(i) = 6;
			break;
		case 'G': 
			group.at(i) = 7;
			break;
		case 'H':
			group.at(i) = 8;
			break;
		case 'I': 
			group.at(i) = 9;
			break;
		case 'J':
			group.at(i) = 10;
			break;
		case 'K': 
			group.at(i) = 11;
			break;
		case 'L':
			group.at(i) = 12;
			break;
		case 'M': 
			group.at(i) = 13;
			break;
		case 'N':
			group.at(i) = 14;
			break;
		case 'O': 
			group.at(i) = 15;
			break;
		case 'P':
			group.at(i) = 16;
			break;
		case 'Q': 
			group.at(i) = 17;
			break;
		case 'R':
			group.at(i) = 18;
			break;
		case 'S': 
			group.at(i) = 19;
			break;
		case 'T':
			group.at(i) = 20;
			break;
		case 'U': 
			group.at(i) = 21;
			break;
		case 'V':
			group.at(i) = 22;
			break;
		case 'W': 
			group.at(i) = 23;
			break;
		case 'X':
			group.at(i) = 24;
			break;
		case 'Y': 
			group.at(i) = 25;
			break;
		case 'Z':
			group.at(i) = 26;
			break;
		default:
			break;
		}
	}

	int multGroup =1;

	int d = group.size();

	for(int i =0;i<=d;i++)
	{
		multGroup *= group.at(i);
	}

	if(multComet%47 == multGroup%47)
		out << "GO\n";
	else
		out << "STAY\n";

	out.close();

	return 0;

}
The index is out of range. This:
 
for(int i =0;i<=a;i++)
should probably be:
 
for(int i =0;i<a;i++)


And the same for the other loop.

You can do the same thing without using loops for this.
Last edited on
You should play around with (e.g.)

int example = 'L' - 'A';

I tried using
for(int i=0;i<a;i++)
but that didn't work either.

@andywestken Could you explain what you wrote there?
Did you fix both loops?
Oh wow, I should have tried to change it for both. Thanks! It's still not working though.

I put
COMETQ
HVNGAT

in the text file, and it should print GO, but it prints STAY. What is wrong with my algorithm.

Also, when i put two words that should output STAY, I get an out of range at memory location error again. For any two words that should output GO, it always outputs STAY. Here is my code again
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

int main()
{
	//char comet[6];
	//char group[6];
	string comet;
	string group;
	ifstream in("ride.txt");
	ofstream out("rideo.txt");

	in >> comet;
	in >> group;

	in.close();

	int a = comet.size();

	for(int i =0;i<a;i++)
	{
		char letter = group.at(i);

		switch(letter)
		{
		case 'A': 
			comet.replace(i, 1, "1");
			break;
		case 'B':
			comet.replace(i, 1, "2") ;
			break;
		case 'C': 
			comet.replace(i, 1, "3") ;
			break;
		case 'D':
			comet.replace(i, 1, "4") ;
			break;
		case 'E': 
			comet.replace(i, 1, "5") ;
			break;
		case 'F':
			comet.replace(i, 1, "6") ;
			break;
		case 'G': 
			comet.replace(i, 1, "7") ;
			break;
		case 'H':
			comet.replace(i, 1, "8");
			break;
		case 'I': 
			comet.replace(i, 1, "9") ;
			break;
		case 'J':
			comet.replace(i, 1, "10") ;
			break;
		case 'K': 
			comet.replace(i, 1, "11") ;
			break;
		case 'L':
			comet.replace(i, 1, "12") ;
			break;
		case 'M': 
			comet.replace(i, 1, "13") ;
			break;
		case 'N':
			comet.replace(i, 1, "14") ;
			break;
		case 'O': 
			comet.replace(i, 1, "15") ;
			break;
		case 'P':
			comet.replace(i, 1, "16");
			break;
		case 'Q': 
			comet.replace(i, 1, "17") ;
			break;
		case 'R':
			comet.replace(i, 1, "18") ;
			break;
		case 'S': 
			comet.replace(i, 1, "19") ;
			break;
		case 'T':
			comet.replace(i, 1, "20") ;
			break;
		case 'U': 
			comet.replace(i, 1, "21") ;
			break;
		case 'V':
			comet.replace(i, 1, "22") ;
			break;
		case 'W': 
			comet.replace(i, 1, "23") ;
			break;
		case 'X':
			comet.replace(i, 1, "24") ;
			break;
		case 'Y': 
			comet.replace(i, 1, "25");
			break;
		case 'Z':
			comet.replace(i, 1, "26");
			break;
		}
	}

	int multComet =1;

	int b = comet.size();

	for(int i =0;i<b;i++)
	{
		multComet *= comet.at(i);
	}

	int c = group.size();


	for(int i =0;i<c;i++)
	{
		char letter = group.at(i);
		switch(letter)
		{
		case 'A': 
			//group.at(i) = 1;
			group.replace(i, 1, "1") ;
			break;
		case 'B':
			group.replace(i, 1, "2") ;
			break;
		case 'C': 
			group.replace(i, 1, "3") ;
			break;
		case 'D':
			group.replace(i, 1, "4") ;
			break;
		case 'E': 
			group.replace(i, 1, "5") ;
			break;
		case 'F':
			group.replace(i, 1, "6") ;
			break;
		case 'G': 
			group.replace(i, 1, "7") ;
			break;
		case 'H':
			group.replace(i, 1, "8") ;
			break;
		case 'I': 
			group.replace(i, 1, "9") ; 
			break;
		case 'J':
			group.replace(i, 1, "10") ; 
			break;
		case 'K': 
			group.replace(i, 1, "11") ; 
			break;
		case 'L':
			group.replace(i, 1, "12") ; 
			break;
		case 'M': 
			group.replace(i, 1, "13") ; 
			break;
		case 'N':
			group.replace(i, 1, "14") ; 
			break;
		case 'O': 
			group.replace(i, 1, "15") ;
			break;
		case 'P':
			group.replace(i, 1, "16") ; 
			break;
		case 'Q': 
			group.replace(i, 1, "17") ; 
			break;
		case 'R':
			group.replace(i, 1, "18") ; 
			break;
		case 'S': 
			group.replace(i, 1, "19") ; 
			break;
		case 'T':
			group.replace(i, 1, "20") ; 
			break;
		case 'U': 
			group.replace(i, 1, "21") ; 
			break;
		case 'V':
			group.replace(i, 1, "22") ; 
			break;
		case 'W': 
			group.replace(i, 1, "23") ; 
			break;
		case 'X':
			group.replace(i, 1, "24") ; 
			break;
		case 'Y': 
			group.replace(i, 1, "25") ; 
			break;
		case 'Z':
			group.replace(i, 1, "26") ; 
			break;
		default:
			break;
		}
	}

	int multGroup =1;

	int d = group.size();

	for(int i =0;i<d;i++)
	{
		multGroup *= group.at(i);
	}

	if(multComet%47 == multGroup%47)
		out << "GO\n";
	else
		out << "STAY\n";

	out.close();

	return 0;

}
Last edited on
Never mind, I fixed it. I also made it more efficient. Instead of replacing each letter in the string with a number, I just multiplied the total and the problem was that when I replaced a letter with a 2 digit number, it took up two spaces, so the loops didnt go all the way to the end.
Topic archived. No new replies allowed.