Currently working on a program that will read a block of test from a .txt file and add three to each character if encrypt is chosen and -3 if decrypt is chosen(their ascii values will increase/decrease by 3) and the result will be printed to CMD. My program works perfectly however if I run it, it prints over itself because the newline chars are not being converted... at least I think.
I'm not sure the way im reading the data in is correct and if it is wrong what should i be using to read it in.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
usingnamespace std;
int main()
{
//data abstraction
char value;
ifstream in;
ofstream out;
string filename,
enc_or_dec;
int char_to_int,
file_counter = 0;
//input: filename and command(encrypt or decrypt)
do{
cout << "Please enter the name of a file: ";
cin >> filename;
in.open(filename.c_str());
file_counter++;
} while(!(in.is_open()) && file_counter <= 2);
if (file_counter <= 3 && in)
{
cout << "Would you like to encrypt or decrypt: ";
cin >> enc_or_dec;
if (!enc_or_dec.compare("encrypt"))
{
while (in.get(value))
{
value = char(int(value) + 3);
cout << value;
Sleep ( 50 ); //used this to realize it was writing over itself
}
}
elseif (!enc_or_dec.compare("decrypt"))
{
while (in.get(value))
{
cout << value;
}
}
else
{
cout << "Incorrect command" << endl;
}
}
else
{
cout << "Filename input number exceeded" << endl;
}
return 0;
}
given an input of...
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium,
totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae
The output should be...
Vhg#xw#shuvslfldwlv#xqgh#rpqlv#lvwh#qdwxv#huuru#vlw#yroxswdwhp#dffxvdqwlxp#groruhptxh#odxgdqwlxp/#
wrwdp#uhp#dshuldp/#hdtxh#lsvd#txdh#de#loor#lqyhqwruh#yhulwdwlv#hw#txdvl#dufklwhfwr#ehdwdh#ylwdh#