Reading from file including newline charactors

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.

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
  
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace 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
                }
            }
            else if (!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#
Last edited on
The problem with just adding 3 is that you may generate none printable characters which may cause an unexpected behavior when printed.
Topic archived. No new replies allowed.