Parts of code being skipped

Okay so I am playing around with simple encryption, in this case XOR. I have put the Encrypting part of the code in a separate file and it works fine, but when i put it with the rest of this it skips inputting the text that one would encrypt.
Please limit you're answers to how to get it to allow me to input text to encrypt. I will worry about better encryption methods and syntax later on.


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 #include <iostream>
#include <fstream>			
using namespace std;


int main()


{
	string option;
	string password;
	string toEncrypt;
	
{


cout << "Do you wish to encrypt something?" << endl;
	cin >> option;

while (option != "yes")    {
		if (option == "no") {
			cout << "okay, please close this program or type 'yes' instead." << endl;
			cin >> option;
		}
		else {
			cout << option + " is not an option. Choose yes or no. (Answers ARE cap sensitive!)" << endl;
			cin >> option;
		}
	}
	cout << "Please enter password: ";
	cin >> password;
	
    while (password != "19966991")	
    {
		if (password != "19966991") {
		cout << "That password is incorrect, please try again" << endl;
		cin >> password;
		}
		else 
		{
		cout << "password correct, encryption process started" << endl;
	}
		}
	}
	
				

  ofstream myfile;
  myfile.open ("Encryption.txt");				
	  char keyToEncrypt = 's';					
{
	 
  cout << "what do you want to encrypt?" << endl;		
  getline (cin, toEncrypt);							

 for (int temp = 0; temp < toEncrypt.size(); temp++)		
   toEncrypt[temp] ^= keyToEncrypt;
 cout << " The encrypted data = " << toEncrypt << endl;				
 myfile << toEncrypt;										
 myfile.close();										

ofstream file;											
file.open("unencryption.txt") ;								

 for (int temp = 0; temp < toEncrypt.size(); temp++)
   toEncrypt[temp] ^= keyToEncrypt; 					
 cout << " The unencrypted data = " << toEncrypt << endl;
	file << toEncrypt;										
}
				
				
	cout << "Encryption successful" << endl;
return 0;
}
Last edited on
Please indent your code and provide an example input.

About your problem, when you input you press <Return> and `getline()' would read precisely to a line break.
So line 54 getline (cin, toEncrypt); would put an empty string on `toEncrypt'.

To avoid that you need to discard that line-break character
You may std::cin.ignore(); before the getline(), or you may do getline (cin>>ws, toEncrypt); to discard any whitespace (careful, if your string starts with spaces those will be discarded)


By the way, notice that you are using formatted output. That may give you issues http://www.cplusplus.com/forum/general/176312/
How do I indent it? (Sorry I'm new to forums in general)

An example output would be:
 
Do you wish to encrypt something?
yes
Please enter password: 19966991
what do you want to encrypt?
 The encrypted data = 
 The unencrypted data = 
Encryption successful



so would getting rid of the <<endl; at the end of the previous line do the same thing? or am I understanding it wrong?

Also it worked fine in the file with just the encrypting which looked like this
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
29
30
31
32
#include <iostream>
#include <fstream>
using namespace std;

int main ()

{
  ofstream myfile;
  myfile.open ("Encryption.txt");
	 
	  string toEncrypt;
	  char keyToEncrypt = 's';
  cout << "what do you want to encrypt? ";
  getline (cin, toEncrypt);
  

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

ofstream file;
file.open("unencryption.txt");

 for (int temp = 0; temp < toEncrypt.size(); temp++)
   toEncrypt[temp] ^= keyToEncrypt;
 cout << " The unencrypted data = " << toEncrypt;
	file << toEncrypt;

	return 0;
}



Also what do you mean by formatted output? (Again sorry i'm new to C++ as well)
Last edited on
> How do I indent it?
your IDE/text editor should have a function for that.
There are also external program that woul do it, I use clang-format
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <fstream>
using namespace std;


int main ()


{
	string option;
	string password;
	string toEncrypt;

	{


		cout << "Do you wish to encrypt something?" << endl;
		cin >> option;

		while (option != "yes")
		{
			if (option == "no")
			{
				cout << "okay, please close this program or type 'yes' instead." << endl;
				cin >> option;
			}
			else
			{
				cout << option + " is not an option. Choose yes or no. (Answers ARE cap sensitive!)" << endl;
				cin >> option;
			}
		}
		cout << "Please enter password: ";
		cin >> password;

		while (password != "19966991")
		{
			if (password != "19966991")
			{
				cout << "That password is incorrect, please try again" << endl;
				cin >> password;
			}
			else
			{
				cout << "password correct, encryption process started" << endl;
			}
		}
	}


	ofstream myfile;
	myfile.open ("Encryption.txt");
	char keyToEncrypt = 's';
	{

		cout << "what do you want to encrypt?" << endl;
		getline (cin, toEncrypt);

		for (int temp = 0; temp < toEncrypt.size (); temp++)
			toEncrypt[temp] ^= keyToEncrypt;
		cout << " The encrypted data = " << toEncrypt << endl;
		myfile << toEncrypt;
		myfile.close ();

		ofstream file;
		file.open ("unencryption.txt");

		for (int temp = 0; temp < toEncrypt.size (); temp++)
			toEncrypt[temp] ^= keyToEncrypt;
		cout << " The unencrypted data = " << toEncrypt << endl;
		file << toEncrypt;
	}


	cout << "Encryption successful" << endl;
	return 0;
}
note how it becomes aparent what is inside which block.


> so would getting rid of the <<endl; at the end of the previous line do the same thing?
No, the problem is with the input buffer, it's completely unrelated to output.

This was your input (\n means that you've pressed <Return>)
yes\n19966991\n
you then do cin >> option; readin an string, and so the input buffer is now \n19966991\n
>> would ignore whitespace, that's why when you do cin >> password; you've got password = "19966991" and not password = "\n19966991"

when is the turn of `getline()' the input buffer only has \n and it "correctly" reads an empty line. To avoid that you need to ignore that character.


> Also what do you mean by formatted output?
perhaps I should have says "text output". Think of it on opposition to binary output.
if you've got int answer = 42;, the output in text mode would be simply 42 but in binary mode it's a sequence of bytes that represents an integer with that value.

When you do toEncrypt[temp] ^= keyToEncrypt; you may end with a value that has not a graphical representation (not a number, letter, symbol) so I'm not sure if you can simply output<<encrypted; and input>>to_decrypt;
but instead you may need to work in binary mode.
Oh wow that's much better :D

oh okay. That makes sense :)

okay sweet i'll try it out thanks!
Topic archived. No new replies allowed.