Jump to content

C++, main isn't displaying a string from function

Hello guys! :) 

Currently I'm in the process of solving a c++ programming exercise for uni but can't really find my mistake. The program is supposed to encrypt a text by combining it with a key via the "^" operator. the encrypted string should then appear on the console. However  my main function does not execute or take the encrypted variable from the encrypt function to display it.

I'm very new to c++ so please be easy with me XD. Thanks for all thhe help! Anyway here's my code so far:

 

#include <iostream>
#include <string>

using namespace std;

 

string encrypt(string text, string key)
{
    string result;
    string part;
    int k;
    
    for (int i = 1; i == text.length(); i++) {
        part = text.at(i) ^ key.at(k);
        
        result = result + part;
    
    if (k == key.length()) 
            k = 1;
         else {
            k++;
        }
    }
    
    return result;
}

/* Hier beginnt das Hauptprogramm */
int main(int argc, char* argv[])
{
    string text = "Dies ist irgendein Text.";
    string key  =  "irgendein Passwort";
    
    string crypt = encrypt(text, key);
    
    cout << "So lautet der verschluesselte Text: " << crypt << endl;
            
    return 0;
}

uni student // at war with Siemens software // wife haver

Link to comment
Share on other sites

Link to post
Share on other sites

 for (int i = 1; i == text.length(); i++) {

 

Shouldn't this be

 

 for (int i = 0; i < text.length(); i++) {

 

Most languages are base 0 and the second expression is sort of "continue while this is true". (I don't know C++, I'm just going on what I've done in C#, PHP, and Java)

I edit my posts a lot.

Link to comment
Share on other sites

Link to post
Share on other sites

hey @MrDrWho13 thats not the problem here, i already tried that. if i would start i at 0 i could only let it run to text.length()-1 because i need the amount of characters of the string.

but thanks anyway :)

uni student // at war with Siemens software // wife haver

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Heinz57 said:

hey @MrDrWho13 thats not the problem here, i already tried that. if i would start i at 0 i could only let it run to text.length()-1 because i need the amount of characters of the string.

but thanks anyway :)

Yeah but the other issue too. Your for loop will never run unless your text string is only 1 character long. https://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

The second condition is when to run the loop, not when to stop it. It needs to be i < text.length() if you're starting at 0 or i <= text.length() if you're starting at 1.

I edit my posts a lot.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Heinz57 said:

hey @MrDrWho13 thats not the problem here, i already tried that. if i would start i at 0 i could only let it run to text.length()-1 because i need the amount of characters of the string.

but thanks anyway :)

Conventionally, you will start the loop at 0 because the first index in an array will be 0. (I just checked and this is also true for C++)

Then you will end the loop at one position before the final one with the less than condition.

If you start your loop at 1, then to iterate over an array you will need to do i-1 every time which is bad practise really.

You'll probably get taught this on your course.

 

EDIT: This will also apply to they way you're accessing the string here. The first character will be index 0 and the last character will be the position length-1.

I edit my posts a lot.

Link to comment
Share on other sites

Link to post
Share on other sites

@MrDrWho13 oh ok thanks alot! i think that helps. i will try to correct and if i fail i will ask again :D 

uni student // at war with Siemens software // wife haver

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×