0

I'm writing a code to encrypt the entered text in Caesar Cipher but I'm getting a problem. While running my code my loop is not being terminated at null character. Code is as follows:

#include <iostream>
using namespace std;

void main()
{
    char message[200], en_message[200];

    cout << "Enter your message to encrypt: ";
    std::cin.getline(message,200);

    for ( int index = 0 ; message[index] != '\0' ; index++ )
    {
        if ( message[index] == 'A' )
            en_message[index] = 'X';

        else if ( message[index] == 'B' )
            en_message[index] = 'Y';

        else if ( message[index] == 'C' )
            en_message[index] = 'Z';

        else
            en_message[index] = message[index] - 3;
    }

        cout << en_message;
}

What I have tried:

1) Using a loop to output array "en_message" with Using " en_message[index] != '\0' " and " en_message[index] != ' ' " as condition in for loop

2) Using an if condition to break the loop.

No matter what I try, I get this output! 1

Any help would be appreciated. Thanks all in advance.

EDIT: Okay, so now I'm getting another problem. I tried the code in G++ compiler on my university's lab computer and it worked but at home I'm receiving this error "Run Time check failure # 2. Stack around variable 'en_message' was corrupt." I'm using Visual Studio 2010 . What could that be? the modified code is:

#include<iostream>
using namespace std;

void main()
{
    char message[200], en_message[200];
    int index;

    cout << "Enter your message to encrypt: ";
    cin >> index;

    for ( index = 0 ; index < 200 ; index++ )
    {
        if ( message[index] == '\0' )
            break;

        if ( message[index] == 'A' )
            en_message[index] = 'X';

        else if ( message[index] == 'B' )
            en_message[index] = 'Y';

        else if ( message[index] == 'C' )
            en_message[index] = 'Z';

        else
            en_message[index] = message[index] - 3;
    }
    en_message[index] = '\0';


    cout << en_message;
}
Aiman Muzafar
  • 385
  • 1
  • 4
  • 11
  • 3
    Take a look at, http://stackoverflow.com/questions/2037209/what-is-a-null-terminated-string. You are forgetting a '\0' at the end of your encrypted string. – Smith_61 Nov 02 '14 at 07:50
  • How are you expecting the output routine to know when to stop? – David Schwartz Nov 02 '14 at 08:26
  • Instead of creating arrays of arbitrary size, why don't you use vectors or strings of just the right size required? – Neil Kirk Nov 05 '14 at 14:55

2 Answers2

1

Your code is doing correct what you are indenting to do. However you are not getting proper output as you are not terminating your character string.

Try this:-

int index;
for ( index = 0 ; message[index] != '\0' ; index++ )

and after this loop,

en_message[index] = '\0';

One more thing you need to ensure is that you have taken into account only uppercase alphabets. So in case of input being lower-case letters program will emit garbage values.

ravi
  • 10,994
  • 1
  • 18
  • 36
0

Just zero-initialize your output buffer and it will work (; Replace this:

char message[200], en_message[200];

with this:

char message[200] = {0}, en_message[200] = {0};
Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58