Ascii code - encryption

A user inputs a string. Characters are translated to the ASCII character 3 values higher. I know you can convert a character into an ascii value by using
 
int('A')


This is what I have... I may be way off. I'd appreciate any help and/or advice
1
2
3
4
5
6
7
8
9
	int len, r,  y,;
	len = line.length();

	for (int i = 0; i < len; i++)
	{
	r = (int)b[i];
	y = r + 3;
	line = line.replace(i, 1, y);
	}	

Last edited on
Here is the 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
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
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>
using namespace std;



int main()
{
string A, E, I, O, U, a, e, i, o, u, line;


	cout << "Enter a line to encrypt" << endl;
	cin >> line;
	string b = 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);

// Other

	int len, r,  y = 0, x = 0;
	len = line.length();

	for (int i = 0; i < len; i++)
	{
	r = (int)b[i];
	y = r + 3;
	line = line.replace(i, 1, y);
	}	
	


	cout << line << endl;


	system("pause");
	return 0;
}


I also have to convert vowels into numbers; which is the first part
To make it a lot simpler, you can just do something around these lines:

1
2
3
4
5
6
7
8
9
10
std::string line = "Hello badger";
	for(int i = 0; i < line.length(); i++) {
		line.at(i) += 3;
	}
	std::cout << "Encrypted: " << line << std::endl;

	for(int i = 0; i < line.length(); i++) {
		line.at(i) -= 3;
	}
	std::cout << "Decrypted: " <<  line << std::endl;


Also, at line 15:
cin >> line;
I suggest using
getline(cin, line);
so it will allow you to use spaces.
Thank you!!! wow that was so much easier than I was making it! However, I used your suggestion on line 15 and it still is not computing spaces
Penquir wrote:
Thank you!!! wow that was so much easier than I was making it! However, I used your suggestion on line 15 and it still is not computing spaces


Try this:

1
2
3
std::string line;
	std::getline(std::cin, line);
	std::cout << line << std::endl;


And make sure that you include the string library, since that's where the getline function comes from.
Topic archived. No new replies allowed.