Help with caesar cipher

Ok so I am currently working on a program that encrypts or decrypts text that is typed in by the user. I am currently having a problem with my program encrypting and decrypting more than one word in a sentence. For example, whenever I try to encrypt the sentence (Life is Good), the program will only encrypt Life. Can anybody here take a look at my code and help me. I would greatly appreciate it.

#include <iostream>
#include <cstring>
using namespace std;

void text_encrypt(char cipher[25], int shift, int num)
{
for (int i=0; i < num; i++)
{
if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
cipher[i] = (char)(((cipher[i] + shift - 'A' + 26) % 26) + 'A');
}
else if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
cipher[i] = (char)(((cipher[i] + shift - 'a' + 26) % 26) + 'a');
}
}
}

void text_decrypt(char cipher[25], int shift, int num)
{
for (int i=0; i < num; i++)
{
if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
cipher[i] = (char)(((cipher[i] - shift - 'A' + 26) % 26) + 'A');
}
else if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
cipher[i] = (char)(((cipher[i] - shift - 'a' + 26) % 26) + 'a');
}
}
}

int main ()
{
char text[10];
static const char encrypt[] = "encrypt";
static const char decrypt[] = "decrypt";
int shift;
char cipher[25];
int result1;
int result2;
int num;



cout << "Enter operation: encrypt or decrypt" << endl;
cin >> text;
cout << "Enter key" << endl;
cin >> shift;
cout << "Enter text to encrypt/decrypt" << endl;
cin >> cipher;

num = strlen (cipher);

result1 = strcmp (text, encrypt);
result2 = strcmp (text, decrypt);

if(result1 == 0)
{
text_decrypt(cipher, shift, num);
}
if(result2 == 0)
{
text_encrypt(cipher, shift, num);
}

cout << "Result:" << endl;
cout << cipher << endl;
}
In main:
1
2
3
4
5
6
7
8
9
    cout << "Enter operation: encrypt or decrypt" << endl;
    cin >> text ;

    cout << "Enter key" << endl;
    cin >> shift;
    cin.ignore() ;  // remove the newline from the stream.

    cout << "Enter text to encrypt/decrypt" << endl;
    cin.getline(cipher, 25) ;
Thank you very much cire. That fixed the problem. I greatly appreciate it.
yeah, I had exactly the same issue when I programmed a simple XOR-Encryption,
the problem is actually, that the "cin >>" operator automatically stops when it encounters a space by design, so that you can input several variables, seperated by spaces like this: cin >> x >> y >>z;.


There's actually a forum post about it. cire already posted the answer,I just wanted to comment on this :D

Please mark this Question as solved, Reclaimer. ..
Topic archived. No new replies allowed.