1

I was writing some code challenge from reddit about encrypting strings and I came up with something like this:

#include <iostream>
#include <string>

using namespace std;

string encrypt(string sentence);

int main()
{
    string sentence;
    int i = 0;
    cout << "Welcome. Enter a sentence: ";
    getline(cin, sentence);
    cout << sentence << endl;
    encrypt(sentence);
    cout << endl << endl;
    system("pause");
    return 0;
}

string encrypt(string sentence)
{

    int i = 0;
    int x = (sentence.size());
    string *encrypted_sentence = new string[sentence.size()];
    int *wsk = new int[sentence.size()];
    for (i = 0; i < x; i++)
    {
        wsk[i] = sentence[i];
    }

    for (i = 0; i < x; i++)
    {
        if (wsk[i] == ' ')
            continue;
        else if (islower(wsk[i]))
        {
            if (wsk[i] <= 99)
                wsk[i] = (wsk[i] + 23);
            else
                wsk[i] = (wsk[i] - 3);
        }
        else
        {
            if (wsk[i] <= 67)
                wsk[i] = (wsk[i] + 23);
            else
                wsk[i] = (wsk[i] - 3);
        }
    }
    for (i = 0; i < x; i++)
    {
        //cout << static_cast <char> (wsk[i]);
        encrypted_sentence[i] = wsk[i];
    }

    return *encrypted_sentence;
}

My problem is, that there is nothing that gets returned. After I run the program I get nothing in return. Can anybody point me in the right direction with this? What have I missed?

4
  • You're returning a std::string in the main (Edit: apparently, i was just confused by the indentation)? (you even declared the return type void, it's usually int to return an error code btw) Why don't you simply print your string with std::cout (Edit: same here)? Commented Aug 25, 2015 at 14:16
  • My function (string encrypt) suppose to return the string. About that std::cout - it's cool, but I would want to do sth other next with this string Commented Aug 25, 2015 at 14:19
  • I think you want this. Commented Aug 25, 2015 at 14:30
  • @Caninonos YES! Thank You very much! Commented Aug 25, 2015 at 14:38

3 Answers 3

2

First main() returns an int always. void main() is not standard. Secondly:

string encrypted_sentence = new string[sentence.size()];

Will not even compile. encrypted_sentence is of type std::string but you are trying to assign to it a std::string *. Third you should avoid using using namespace std;

Update:

I believe you are trying to output the encrypted string at:

cout << endl << endl;

But all this is doing is outputting 2 newlines and flushing the output twice. If you want to display the encrypted string then you either need to capture the return of the encrypt() function and display it or encrypt() can take the string in by reference. IF you change encrypt() to take a reference then it would become:

void encrypt(string & sentence)
{
    string *encrypted_sentence = new string[sentence.size()]; // get rid of this line as it is not needed.
    //...
    for (i = 0; i < x; i++)
    {
        sentence[i] = wsk[i];
    }
}

And then you would output the string with:

cout << sentence << endl;
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry. I forgot to change it back before posting it here (I tried a lot of stuff). It's: string *encrypted_sentence = new string[sentence.size()]; of course.
@T.T. Then you should edit your question with what you actually have.
Done. Sorry. New here.
Great! I'll try it this way now. Thank You!
0

In case anyone would seek an answer to this question, I've come up with this, and I'm pretty sure it finally works how I wanted it to:

#include <iostream>
#include <string>

std::string encrypt(std::string to_encrypt);

int main()
{
std::string sentence;
std::string result;
std::cout << "Welcome. Please enter a sentence: ";
getline(std::cin, sentence);
result = encrypt(sentence);
std::cout << "Result: " << result << std::endl;
system("pause");
return 0;
}

std::string encrypt(std::string to_encrypt)
{
int i = 0;
int x = (to_encrypt.size());
std::cout << std::endl << "x = " << x << std::endl;
int *temp = new int[to_encrypt.size()];
for (i = 0; i < x; i++)
{
    temp[i] = to_encrypt[i];
}

for (i=0; i < x; i++)
{
    if (temp[i] == ' ')
        continue;
    else if (islower(temp[i]))
    {
        if (temp[i] <= 99)
            temp[i] = temp[i] + 23;
        else
            temp[i] = temp[i] - 3;
    }
    else
    {
        if (temp[i] <= 67)
            temp[i] = temp[i] + 23;
        else
            temp[i] = temp[i] - 3;
    }
}
std::string encrypted;
for (i = 0; i < x; i++)
{
    encrypted += (static_cast <char> (temp[i]));
}
return encrypted;

}

Comments

-1

The code is obviously wrong. string *encrypted_sentence = new string[sentence.size()]; allocates an ARRAY of strings! Not a single string. Judging from that, you can see how your code is wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.