First, let's talk a bit about style. The way your code looks is extremely important to readability, not just for yourself but for others. There are tools that can style code for you, like clang-format. Here's your code with the only change being that I ran your code through clang-format set to Google's style. I chose this style because it seems good for presentation on this site.
Whether you use a tool or not, being aware of and using good and consistent style is important.
#include <ctime>
#include <iostream>
#include <string>
std::string encryptFunc(std::string address) {
int i = 0;
int store = address.length();
std::string Output;
while (i < store) {
if (address[i] == ' ' || address[i] == 'a' || address[i] == 'b' ||
address[i] == 'c' || address[i] == 'd' || address[i] == 'e' ||
address[i] == 'f' || address[i] == 'g' || address[i] == 'h' ||
address[i] == 'i' || address[i] == 'j' || address[i] == 'k' ||
address[i] == 'l' || address[i] == 'm' || address[i] == 'n' ||
address[i] == 'o' || address[i] == 'p' || address[i] == 'q' ||
address[i] == 'r' || address[i] == 's' || address[i] == 't' ||
address[i] == 'u' || address[i] == 'v' || address[i] == 'w' ||
address[i] == 'x' || address[i] == 'y' || address[i] == 'z' ||
address[i] == 'A' || address[i] == 'B' || address[i] == 'C' ||
address[i] == 'D' || address[i] == 'E' || address[i] == 'F' ||
address[i] == 'G' || address[i] == 'H' || address[i] == 'I' ||
address[i] == 'J' || address[i] == 'K' || address[i] == 'L' ||
address[i] == 'M' || address[i] == 'N' || address[i] == 'O' ||
address[i] == 'P' || address[i] == 'Q' || address[i] == 'R' ||
address[i] == 'S' || address[i] == 'T' || address[i] == 'U' ||
address[i] == 'V' || address[i] == 'W' || address[i] == 'X' ||
address[i] == 'Y' || address[i] == 'Z') {
srand(time(NULL));
int randJargonSelect =
rand() % 26; // which character of individual jargon is currently
// selected to be placed
char jargonSelection[] = {'1', '2', '3', '?', '<', '~', '@', '/', '#',
'+', '*', '&', '^', '%', '$', '!', '-', '0',
'9', '8', '7', '6', '5', '4', '`', '|'};
char randPlacementJargon =
jargonSelection[randJargonSelect]; // the variable holder for the
// character of jargon to place
int randJargonAmount =
rand() % 500; // the amount of jargon chars to place
Output = address[i], randPlacementJargon * randJargonAmount;
}
std::cout << Output;
i++;
};
std::string nada = " ";
return nada;
}
int main() {
std::string userin;
std::cout << "Please enter the message you wish to encrypt" << std::endl;
std::cout << "Enter here: ";
std::getline(std::cin, userin);
std::cout << " " << std::endl;
std::cout << "Here is your Encoded Message: " << encryptFunc(userin)
<< std::endl;
std::cout << " " << std::endl;
return 0;
Now, let's look at your code. I'll start with the main function since it's smaller. There are two small things here, and one can be considered somewhat subjective. The line std::cout << " " << std::endl; is wholly unnecessary. If you just want a blank line, print '\n' (newline character). And you wouldn't need an entire statement for that, either, just stick it at the beginning of your next actual cout. std::cout << "\nHere is your Encoded Message: " << .... The second thing is to prefer '\n' over std::endl. std::endl does two things, print the '\n' and flush the stream. You don't have to constantly flush the stream.
This is note in your main, but it transitions us. You attempt to print the result of encryptFunc() but it returns a single space, the one you declare and return at the end of the function. Your function also prints, and this is bad.
Functions should be singular in purpose. You named it encryptFunc(), not encryptAndPrintFunc(). You also don't need to name your function with any form of "func." We all know it's a function. And you're not really encrypting, I'd say it's more like padding, so I'm going to change the name.
Let's also look at the parameter name, address. That makes no sense. You're not passing an address, your passing a message to be encoded. We'll change that name, I'm choosing message.
std::string pad(std::string message) { ... }
So we've got this, and it already is more understandable what your code intends to do.
I'm going to rename Output to output. Names starting with an upper case letter is generally reserved for classes/structs. You want to pad after a space or a letter. You have a massive Boolean expression for your if. We're going to change this loop to a for loop, and simplify things with the library <cctype>. This is a great library for ASCII work. If you don't know what ASCII is, look up an ASCII table. For beginner programming stuff, ASCII is great. The loop change means we don't need i (not the way you use it anyway) or store.
std::string pad(std::string message) {
std::string output;
for (auto c : message) {
if (std::isalpha(c) || std::isspace(c)) {
// Still working
The for loop I'm using is a range-based for loop. The value of i will be each char of message. Next we're going to change out the older C-style random number stuff for C++ versions. This involves <random>.
std::string pad(std::string message) {
std::string output;
const std::string jargon("123456790?<>~@/#+*&^%$!-`|");
static std::mt19937 prng(std::random_device{}());
static std::uniform_int_distribution<std::size_t> jargonSelector(
0, jargon.length() - 1);
static std::uniform_int_distribution<int> numJargon(1, 5);
for (auto c : message) {
if (std::isalpha(c) || std::isspace(c)) {
// Still working
The keyword static is there for situations when you might call this function many times. If that's the case, you don't want to constantly re-initialize your Pseudo-Random-Number-Generator or distributions. static is how we accomplish that. const ensures that jargon is never modified.
Now, we're getting to the actual issue of your code. This line: output = message[i], randPlacementJargon * randJargonAmount; doesn't do what you expect. The comma operator performs the first expression, discards the result, and returns the result of the second expression. So, you made output hold a single character, then did a random multiplication that wasn't saved anywhere. That's why the original string printed again, your function printed it one character at a time.
You could have known that statement was not working as intended if you were compiling with warnings enabled. -Wall -Wextra should be considered mandatory. Let the compiler help you.
Here's the full program:
#include <iostream>
#include <random>
#include <string>
std::string pad(std::string message) {
std::string output;
const std::string jargon("123456790?<>~@/#+*&^%$!-`|");
static std::mt19937 prng(std::random_device{}());
static std::uniform_int_distribution<std::size_t> jargonSelector(
0, jargon.length() - 1);
static std::uniform_int_distribution<int> numJargon(1, 5);
for (auto c : message) {
if (std::isalpha(c) || std::isspace(c)) {
int padSize = numJargon(prng);
output.push_back(c);
for (int i = 0; i < padSize; ++i) {
output.push_back(jargon[jargonSelector(prng)]);
}
}
};
return output;
}
int main() {
std::string userin;
std::cout << "Please enter the message you wish to encrypt" << '\n';
std::cout << "Enter here: ";
std::getline(std::cin, userin);
std::cout << "\nHere is your Encoded Message: " << pad(userin) << '\n';
std::cout << " " << '\n';
return 0;
}
Once a character is determined to be a letter or space (worth noting that std::isspace returns true for any whitespace character): the size of the pad is determined, the original letter is pushed back into output, then a small loop to push back the pad size's worth of 'jargon' characters.
At the end of the function, the object output is returned instead of the single space nothing string you were returning. I also removed the print statement from the function because that's not the function's job.
Here's a sample output:
Please enter the message you wish to encrypt
Enter here: Hello World!
Here is your Encoded Message: H@32e~/l94l%-4o5`> ?#W/o*5!r>!l&+@d15~3