Space causes problems in a string! Why?

Hi there,

I'm working on a small XOR-Encryption program that's based on this article.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>
#include <cstdio> 

using namespace std;

/*
!!! source: http://www.cplusplus.com/articles/Ly86b7Xj/ !!!
*/

int main (int argc, char** argv){

 string toEncrypt;
 cin >> toEncrypt;
 char keyToEncrypt = 's'; //remember 115 in ascii

 for (int temp = 0; temp < toEncrypt.size(); temp++)
   toEncrypt[temp] ^= keyToEncrypt;
 cout << "\nThe encrypted data = " << toEncrypt;

 for (int temp = 0; temp < toEncrypt.size(); temp++)
   toEncrypt[temp] ^= keyToEncrypt; //notice we're using the exact same key, to unencrypt the data.
 cout << "\nThe unencrypted data = " << toEncrypt;
 cout << "\n";

return 0;

}


Works fine with single words, but as soon as you want to encrypt a sentence, it does not work :( (try a single word, then try "This is a test", which will only en- and decrypt This . Why? Can you please help me?

Greetings, Lukas
1
2
3
string toEncrypt;
//cin >> toEncrypt;
std::getline( std::cin, toEncrypt ) ;

http://www.cplusplus.com/reference/string/string/getline/

dooooo works perfectly, thank you VERY much for your fast answer!

Lukas


P.S.: Whats the problem with cin >> ?
There is no problem with the extraction operator>>, it just stops processing the string when it encounters a white space character by design.
kk, tyvm :)
Topic archived. No new replies allowed.