So, I've been through a class in java so I know a little bit of what I'm doing...

The assignment I have is to create a program that takes a string and converts the letters four away in the alphabet (ie: a becomes e and so forth).

I've gotten most of my kinks worked out on my own, but you'll notice my encode and decode are different, I had it set up the way I do for my decode at first and switched to the method I am using in my encode in an attempt to fix my problem with working backwards. now in order to fix my decode I need to fix my circular problem with going past 'a' or 'z' and coming around to the other side of the alphabet.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int choice = 0;
    string input;
    cout <<"would you like to: \n 1. Decode a message \n 2. Encode a message \n";
    cin >> choice;
    if (choice == 2)
    {

        int count = 0, length;
        cin.ignore(10000,'\n');
        cout << "enter the phrase to be encoded: \n";
        getline(cin, input);

        length = (int)input.length();

        for (count = 0; count < length; count++)
        {
            if (isalpha(input[count]))
            {
                input[count]-= 4;
                if (input[count] > 'z')
                {
                    input[count] = 'a' + ('z' - input[count]) -1;
                }
            }
        }
    } 
    if (choice == 1)
    {

        int count = 0, length;
    cin.ignore(10000,'\n');
    cout << "enter the phrase to be encoded: \n";
    getline(cin, input);

    length = (int)input.length();

    for (count = 0; count < length; count++)
    {
        if (isalpha(input[count]))
        {
            for (int i = 0; i < 22; i++)
            {
                if (input[count] == 'a')
                    input[count] == 'z';
                else input[count]++;
            }
        }
    }
    }

    cout << "Results: \n " << input  << endl;
    int (x) = 0;
    cin >> x;
}

any help provided would be greatly appreciated. :)

There are a number of bugs in your code. Much of which you could easily figure out by simply adding a print out in the midst of the encoding/decoding loops and examine what happens when you try it.

First, at line 51, you have input[count] == 'z'; where it should obviously be input[count] = 'z';, notice the single equal sign.

Then, you seem to have used + instead of - everywhere, and vice versa. That is, at line 52, you should have input[count]--;. Then, at line 26, you should have input[count] += 4;. And finally, at line 29, you should have input[count] = 'a' + (input[count] - 'z') - 1;.

It seems quite obvious to me that the encoding method (lines 12 to 33) is much nicer than the decoding method.

And if you have further issues, just test it and observe the result, that's the best way to wield out a bug or a problem. So, to recap, the encoding method should probably look like this:

if (choice == 2)
{
    cin.ignore(10000,'\n');
    cout << "enter the phrase to be encoded: \n";
    getline(cin, input);
    for(int count = 0; count < input.length(); count++)
    {
        if (isalpha(input[count]))
        {
            input[count] += 4;
            if (input[count] > 'z')
            {
                input[count] = 'a' + (input[count] - 'z') - 1;
            }
        }

        // For debugging purposes:
        cout << "After encoding letter " << count << ", got this: " << input << endl;

    }
}

and similarly for the decoding, by flipping everything around (plus becomes minus, etc..).

thank you a lot, I was working on this for a while trying diffrent things to get it to work so my code was beyond a little messy, sorry about that, I was headed to bed and figured I could use some help with this when I woke up.

so now I fixed my encoding thanks to you. the decoding still won't work with 'abcd' and I'm not sure why,

for (int count = 0; count < input.length(); count++)
        {
            if (isalpha(input[count]))
            {
                input[count] -= 4;
                if (input[count] < 'a')
                {
                    input[count] = 'z' - (input[count] - 'a') + 1;
                }
            }
            cout << "After decoding letter " << count << ", got this: " << input << endl;
        }

I need to have this work for capitols as well as have the program able to encode a message and decode it as well by repeating the whole program, not really sure what loop I should use for that though. Here is where I am with just the encoding fixed.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int choice = 0;
    string input;
    cout <<"would you like to: \n 1. Decode a message \n 2. Encode a message \n";
    cin >> choice;
    if (choice == 2)
    {


        cin.ignore(10000,'\n');
        cout << "enter the phrase to be encoded: \n";
        getline(cin, input);



        for (int count = 0; count < input.length(); count++)
        {
            if (isalpha(input[count]))
            {
                input[count]+= 4;
                if (input[count] > 'z')
                {
                    input[count] = 'a' + ( input[count] - 'z') -1;
                }
            }
             cout << "After encoding letter " << count << ", got this: " << input << endl;
        }
    } 
    if (choice == 1)
    {


    cin.ignore(10000,'\n');
    cout << "enter the phrase to be decoded: \n";
    getline(cin, input);

    for (int count = 0; count < input.length(); count++)
    {
        if (isalpha(input[count]))
        {
            input[count] -= 4;
            if (input[count] < 'a')
            {
                input[count] = 'z' - (input[count] - 'a') + 1;
            }
        }
         cout << "After encoding letter " << count << ", got this: " << input << endl;
    }
    }

    cout << "Results: \n " << input  << endl;
    cout << "Press any key and enter to encode/decode another message \n" ;
    int (x) = 0;
    cin >> x;
}

Edited 3 Years Ago by Jed_1

Your decoding can be fixed by flipping the subtraction at line 50, getting this:

input[count] = 'z' - ('a' - input[count]) + 1;

As for capital letters, that's not any different than for small letters. You just have to check before whether the letter is a capital letter or not. The "isalpha" function already checks if it is a letter (small or big), you can just replace that with a test for small letter and a test for big letter:

        if((input[count] >= 'a') && (input[count] <= 'z'))
        {
            input[count]+= 4;
            if (input[count] > 'z')
            {
                input[count] = 'a' + ( input[count] - 'z') -1;
            }
        } 
        else if((input[count] >= 'A') && (input[count] <= 'Z'))
        {
          // same strategy as above, but with 'A' and 'Z'.
        };

Okay, thanks a lot to mike 2000 17!
I got it all working just the way I need it and it was simpler than I thought it would be, I saw a lot of code online for coding a whole new alphabet into the code and telling it to only use that, but this is a great solution.

thank you again! and here is the final working product. I finished this so early (not due for a few days) I'm gonna see if I can work out coding in a shift varible for fun. :)

nevermind, I added a shift varible and it only took all of 4 minutes. lol

thanks again mike

final working code.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int x = 1;
    while( x = 1 )
    {
        int v;
        int choice = 0;
        string input;
        cout <<"would you like to: \n 1. Decode a message \n 2. Encode a message \n";
        cin >> choice;
        cin.ignore(10000,'\n');
        cout <<"how many letter places would you like to shift your phrase? \n";
        cin >> v;
        if (choice == 2)
        {
            cin.ignore(10000,'\n');
            cout << "enter the phrase to be encoded: \n";
            getline(cin, input);

            for (int count = 0; count < input.length(); count++)
            {
                if (isalpha(input[count]))
                {
                    if((input[count] >= 'a') && (input[count] <= 'z'))
                    {
                        input[count]+= v;
                        if (input[count] > 'z')
                        {
                            input[count] = 'a' + ( input[count] - 'z') -1;
                        }
                    }
                    else  if((input[count] >= 'A') && (input[count] <= 'Z'))
                    {
                        input[count]+= 4;
                        if (input[count] > 'Z')
                        {
                            input[count] = 'A' + ( input[count] - 'Z') -1;
                        }
                    }
                }
            }
        } 
        if (choice == 1)
        {
            cin.ignore(10000,'\n');
            cout << "enter the phrase to be decoded: \n";
            getline(cin, input);

            for (int count = 0; count < input.length(); count++)
            {
                if (isalpha(input[count]))
                {
                    if((input[count] >= 'a') && (input[count] <= 'z'))
                    {
                        input[count] -= v;
                        if (input[count] < 'a')
                        {
                            input[count] = 'z' - ('a' - input[count]) + 1;
                        }
                    }
                    else if ((input[count] >= 'A') && (input[count] <='Z'))
                    {
                        input[count] -= 4;
                        if (input[count] < 'A')
                        {
                            input[count] = 'Z' - ('A' - input[count]) + 1;
                        }
                    }
                }
            }
        }

        cout << "Results: \n " << input  << endl;
        cout << "Enter 1 to encode/decode another message \n" ;
        cin >> x;
    }
}

Edited 3 Years Ago by Jed_1

This question has already been answered. Start a new discussion instead.