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;
}