Hi, so i'm new to programming. This is my first course! So i'm new and still learning. I feel like I'm heading the right direction but I could be wrong. I don't know how to encode and decode. I need to encode it with the key from user, such as add an int 1-35. My assignment instructions are
"Write a C++ program that encodes the user’s string so that he can send a “secret message” to his friends. In order to encode the string, we will use an integer offset value known as the codeInteger or encryption key. The encryption key must be between 1 and 35, inclusive. It is added to each character’s ASCII (decimal) value thus making it a new ASCII character. The letter “A” is ASCII 65. If we add 10 to it, it becomes ASCII 75,which is the letter K. Remember that spaces are also characters and should be encoded. Refer to an ASCII Character Code set to see a complete set of decimal/character values.
Secret Message: MEET ME AT 8
Each character M E E T M E A T 8
ASCII values 77 69 69 84 32 77 69 32 65 84 32 56
Key = 10, add 87 79 79 94 42 87 79 42 75 94 42 66
Encoded message: W O O ^ * W O * K ^ * B
In your main function, show your header, seed the rand function and begin a do while loop. Here is the flow:
• Call GetCodeInteger, which returns the int. The function gives the user the option to input a codeInteger or have it randomly determined by the program. If the user inputs an integer, the program checks that it is in the valid range. If not, display a message and loop back to input a new key.
• Ask the user whether he/she wants to Encode or Decode a secret message. Hint: To encode/decode you’ll need to access each character in the string, adjust it, and then either return the changed string or use a stringstream object to obtain the new string.
Encode,
Call AskForString, which returns the secret message to be encoded. Require that all letters be upper case, and that no punctuation be used.
Call Encoder, passing the codeInteger and secret message, and returning the encrypted message.
Display the message, the encrypted message, and the codeInteger, or encryption key.
Decode,
Call AskForEncryptedMessage, which returns the encrypted message to be decoded.
Call Decoded, passing the codeInteger and encrypted message, and returning the decoded message.
Display the encrypted message, the decoded message and the codeInteger, or encryption key.
• Ask the user if he/she wants to try another secret message
• When the user has finished, display a good-bye message"
my code is
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
|
#include <iostream>
#include <string>
using namespace std;
string doAnother;
string AskForString;
string Encoder(int key, string message);
string Decoder(int key, string message);
string AskForEncrypted;
int GetCodeInteger, choice, ascChar;
int main()
{
//Seed random
srand(time(NULL));
do
{
//Ask user if they have a code integer or want program to determine one for them
cout << "Enter your code integer 1-35 or enter 0 to have program determine a random one: ";
cin >> GetCodeInteger;
//if out of range
if (GetCodeInteger < 0 || GetCodeInteger > 35)
{
cout << "Out of range!" << endl;
}
else
{
//give user random number
if (GetCodeInteger == 0)
{
GetCodeInteger = rand() % 35 + 1;
cout << "Your given code integer is: " << GetCodeInteger << endl;
}
//Ask if user wants to encode or decode
cout << "Would you like to Encode or Decode? 1 for Encode or 2 for Decode: ";
cin >> choice;
cin.ignore();
//Encode
if (choice == 1)
{
cout << "Please enter your message, all upper case & no punctuation: ";
getline(cin, AskForString);
for (int i = 0; i < AskForString.length(); i++)
{
ascChar = AskForString[i];
cout << ascChar << endl;
}
}
//Decode
else if (choice == 2)
{
}
}
// loop back if do another
cout << "Would you like to do another one? yes/no: ";
cin >> doAnother;
} while (doAnother == "yes");
cout << "Goodbye!" << endl;
return 0;
}
| |