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.
#include <iostream>
#include <string>
usingnamespace 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;
}
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
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.
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?
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.