Having problems with a simple encryption program

Hello,
I'm having a lot of trouble programming an encryption program. It has to have a menu that loops and allows the user to do five things. Encrypt an entire word rotation(rotate string). Decrypt an entire word rotation. Encrypt each letter rotation(caesar cipher). Decrypt the letter rotation. And exit the program when they're done. I'm really having a hard time with the encryption. I've tried making a for loop but it doesn't seem to effect the string at all. I've only been coding for a few weeks. So any help would be apperciated.
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
 #include <iostream>
#include <string>
using namespace std;
int main()
{
int option;
do 
{
 	cout << "What would you like to do?\n";
 	cout << "1.Encrypt entire word rotation\n";
 	cout << "2.Decypt entire word rotation\n";
 	cout << "3.Encrypt each letter rotation\n";
 	cout << "4.Decrypt each letter rotation\n";
 	cout << "5.Exit\n";
 	cin >> option;
int key;
string word;



	switch(option)

{
	case 1 : cout << "Enter word you would like encrypted?\n";
				cin >> word;
				cout << "Enter your key\n";			
				cin >> key; 
				
				 for (int index = 0; index <=word.length();index++)
					{	
					
					 (word[index + key])% word.length();
				 
					 					 																 												}																	
				  	
				  	cout << "This is an encrypted word rotation " << word << endl;
				    break;

			
			
	case 2 : cout << "Enter word to be decrypted" << endl;			
		cin >> word; 
			cout << "Enter decryption key\n";
			cin >> key;
			
			
			cout << "This is a decrypted  word rotation " << word << endl;
		break;
	case 3 : 
			 cout <<"Enter word to be encrypted\n";
			 cin >> word; 
			 cout << "Enter key \n";
			 cin >> key ;
			  for (int index = 0; index< word.length(); index++)
					{	
					(word[index ]+ key) % word.length();	

					}							

			cout << "This is a encrypted letter rotation " << word << endl;

		break;


	case 4: cout << "This is a decrypted letter rotation \n";
		break;
	case 5: cout << "Goodbye\n"; 
					  return 0;
	default : cout << "Invalid entry shutting down\n";
					return 0; 

}


}	
 	while (option<5);



return 0;	
}
(word[index + key])% word.length(); This does nothing.

I guess you want to store it in the string.
word[index] = word[index + key] % word.length();

Be careful index + key might lead to an index out of range.
What's the algorithm to encrypt the string?
closed account (48T7M4Gy)
I'm assuming you haven't got to functions yet, but this might be useful

To encrypt:
word[i] = char('a' + (word[i] - 'a' + key) % 26);

To decrypt you encrypt with the complement of the key, ie 26 -key%26, so
word[i] = char('a' + (word[i] - 'a' + 26 - key % 26) % 26);

This will over-write your original word.
What's the algorithm to encrypt the string?

That's what I'm having trouble with. Kemort solution works for option 3 and 4 but I need to move the letters in the string to the right without going over the index of the original word. For option 1 and 2.
Example:
Enter word to be encrypted:
Secret
Enter key:
2
Encrypted word is :
etsecr
closed account (48T7M4Gy)
Well, that one is quite easy to encrypt:
1. make a copy of the word
2. transfer characters across to the copy in the shifted position, using the length as the modular arithmetic base. The letters stay the same, only the position eg word[i] -> copy[(i+key ...) ... ]

Use tolower() function to change to lower case, and toupper() to change to uppercase if the first character is uppercase as a rule, otherwise just change all characters to lowercase.

closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// ENRYPTION BY ROTATATION WITHIN WORD
    std::string word = "Secret";
    std::string cipher = word;
    size_t length = word.length();
    
    
    // ENCRYPT BY ROTATION OF WORD
    for(size_t i = 0; i < length; ++i)
        cipher[(i + key) % length] = tolower(word[i]);
    
    std::cout << word << ' ' << cipher << '\n';
    
    
    // DECRYPT BY ROTATION OF WORD
    std::string plain = cipher;
    for(size_t i = 0; i < length; ++i)
        plain[(length + i - key % length) % length] = tolower(cipher[i]);
    
    std::cout << cipher << ' ' << plain << '\n';
I tried your solution but I can only get it to display the unmodified string. Can you tell me what I'm doing wrong?



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


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int option;
do 
{
 	cout << "What would you like to do?\n";
 	cout << "1.Encrypt entire word rotation\n";
 	cout << "2.Decypt entire word rotation\n";
 	cout << "3.Encrypt each letter rotation\n";
 	cout << "4.Decrypt each letter rotation\n";
 	cout << "5.Exit\n";
 	cin >> option;
int key;
string word;
string cipher = word; 
size_t length = word.length();


	switch(option)

{
	case 1 : cout << "Enter word you would like encrypted?\n";
				cin >> word;
				cout << "Enter your key\n";			
				cin >> key; 
				     
           				for (size_t index = 0; index < length; ++index)
           				{	 cipher[(index + key) % length] = tolower(word[index]);
    
  							cout << "This is an encrypted word" << word << ' ' << cipher << '\n';
           				}	
  							    break; 				
																	  				
	case 2 : cout << "Enter word to be decrypted" << endl;			
		cin >> word; 
			cout << "Enter decryption key\n";
			cin >> key;
			rotate(word.begin(), word.begin() - (key %word.length()), word.end());
			
			cout << "This is a decrypted  word rotation " << word << endl;
		break;
	case 3 : 
			 cout <<"Enter word to be encrypted\n";
			 cin >> word; 
			 cout << "Enter key \n";
			 cin >> key ;
			  for (int index = 0; index< word.length(); index++)
					{	
					word[index] = char('a' + (word[index] - 'a' + key) % 26);	

					}							

			cout << "This is a encrypted letter rotation " << word << endl;

		break;


	case 4: cout << "Enter word you would like decrypted\n";
			cin >> word;
			cout << "Enter your key\n";
			cin >> key;
			for (int index = 0; index<= word.length(); index++)
			{
				word[index] = char('a' + (word[index] - 'a' + 26 - key % 26) % 26);
			}
			cout << "This is a decrypted letter rotation " << word << endl;
		break;
	case 5: cout << "Goodbye\n"; 
					  return 0;
	default : cout << "Invalid entry shutting down\n";
					return 0;



 


}


}	
 	while (option<5);


return 0;	
}




Have a closer look at your for loop on line 33. What value does length have ?
Thank you Thomas. I got it working by placing length and cipher after the user inputs a value for word. What value is a string set to if you don't define it or let a user input a value?
closed account (48T7M4Gy)
What value is a string set to if you don't define it or let a user input a value?


Best idea is to try it. cout << word.length() << '\n; and see what you get. Copy the line into your program anywhere you want to (after word has been declared, that is) and see for yourself.

I've only been coding for a few weeks.
Using that method is a good tip for your future debugging career. You can delete the debugging line(s) in the final production version :)

Using that method is a good tip for your future debugging career.
.
I'll keep that in mind. Looks like the terminal is giving me a value of zero. Which makes sense. When I replaced length with word.length(),(size_t index = 0; index < word.length(); ++index) It was giving me a floating point error. This has been extremely helpful. I can't thank you enough.
Topic archived. No new replies allowed.