Can't find error

Trying to create a encryption code. Vowels change to numbers. ex: A or a = 1. It does not have a small error with the red sqiggles, it starts to run and stops after input. I would appreciate any help..

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 <string>
#include <cctype>
#include <sstream>
using namespace std;

	string A, E, I, O, U, a, e, i, o, u, line, line2;

int main()
{


	cout << "Enter a line to encrypt" << endl;
	cin >> line;
	
	//vowels
	int loc;
	do{
		loc = line.find("a");
		line = line.replace(loc, 1, "1");
	} while (loc != -1);

	do{
		loc = line.find("A");
		line = line.replace(loc, 1, "1");
	} while (loc != -1);

	do{
		loc = line.find("e");
		line = line.replace(loc, 1, "2");
	} while (loc != -1);

	do{
		loc = line.find("E");
		line = line.replace(loc, 1, "2");
	} while (loc != -1);

	do{
		loc = line.find("i");
		line = line.replace(loc, 1, "3");
	} while (loc != -1);

	do{
		loc = line.find("I");
		line = line.replace(loc, 1, "3");
	} while (loc != -1);

	do{
		loc = line.find("o");
		line = line.replace(loc, 1, "4");
	} while (loc != -1);

	do{
		loc = line.find("O");
		line = line.replace(loc, 1, "4");
	} while (loc != -1);

	do{
		loc = line.find("u");
		line = line.replace(loc, 1, "5");
	} while (loc != -1);

	do{
		loc = line.find("U");
		line = line.replace(loc, 1, "5");
	} while (loc != -1);
	
	cout << line;

	system("pause");
	return 0;
}
Last edited on
1
2
3
4
5
do{
		loc = line.find("a");
		line = line.replace(loc, 1, "1");
		cout << line[loc] << endl;
	} while (loc != -1);


Its becuase. Say I input Tarik. First loop it will find 1 "a" and loc will be equal to one. Next loop, it will not find an "a" so loc is now -1. Then when you try and do this line = line.replace(loc, 1, "1"); Its suddenly out of bounds. Because loc is -1. And there is no memory at -1 index.

Edit: You can solve this by adding this simple if statement so it never goes out of bounds
if (loc != -1)

P.S. Please dont use global variables. Cant even debug the program properly...
Full Program:

Input : Tarik
Output: T1r3k


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
string A, E, I, O, U, a, e, i, o, u ,line, line2;

int main()
{

	

	cout << "Enter a line to encrypt" << endl;
	cin >> line;

	//vowels
	int loc;
	do{
		loc = line.find("a");
		if (loc != -1)
			line = line.replace(loc, 1, "1");
		
	} while (loc != -1);

	do{
		loc = line.find("A");
		if (loc != -1)
		line = line.replace(loc, 1, "1");

	} while (loc != -1);

	do{

		loc = line.find("e");
		if (loc != -1)
		line = line.replace(loc, 1, "2");
	} while (loc != -1);

	do{
		loc = line.find("E");
		if (loc != -1)
		line = line.replace(loc, 1, "2");
	} while (loc != -1);

	do{
		loc = line.find("i");
		if (loc != -1)
		line = line.replace(loc, 1, "3");
	} while (loc != -1);

	do{
		loc = line.find("I");
		if (loc != -1)
		line = line.replace(loc, 1, "3");
	} while (loc != -1);

	do{
		loc = line.find("o");
		if (loc != -1)
		line = line.replace(loc, 1, "4");
	} while (loc != -1);

	do{
		loc = line.find("O");
		if (loc != -1)
		line = line.replace(loc, 1, "4");
	} while (loc != -1);

	do{
		loc = line.find("u");
		if (loc != -1)
		line = line.replace(loc, 1, "5");
	} while (loc != -1);

	do{
		loc = line.find("U");
		if (loc != -1)
		line = line.replace(loc, 1, "5");
	} while (loc != -1);

	cout << line;
	
	system("pause");
	return 0;
}
Last edited on
Thank you so much!
My pleasure :)
Topic archived. No new replies allowed.