> 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.