I'm trying to finish up this little shift cipher I'm writing so I can encrypt the text files on my flash drive in case the drive becomes lost or stolen. It simply reads line by line from a plain text file, encrypts each line and outputs the lines to a cipher text file.

The problem is that it only works for the last paragraph of the plain text file.

Input C:\test\plain.txt

The world’s richest and probably most famous athlete smashes into a fire hydrant and then a tree less than a pitch shot away from his own driveway. At 2:25 a.m. And then is reportedly rescued by his wife, who tells police she used a golf club to smash the rear windows of the car. And then thrice rebuffs visits from the police to talk about the accident, including most recently Sunday afternoon.

The two, extremely terse statements about the incident posted by the Woods camp on tigerwoods.com did little to satisfy the public’s interest. On the contrary, the statements only egged on the world’s amateur sleuths. What really happened, and why? The puzzle is irresistible.

Output C:\test\cipher.txt

è
<PM\_WM`\ZMUMTa\MZ[M[\I\MUMV\[IJW]\\PMQVKQLMV\XW[\MLJa\PM?WWL[KIUXWV\QOMZ_WWL[KWULQLTQ\\TM\W[I\Q[Na\PMX]JTQKz[QV\MZM[\7V\PMKWV\ZIZa\PM[\I\MUMV\[WVTaMOOMLWV\PM_WZTLz[IUI\M]Z[TM]\P[?PI\ZMITTaPIXXMVMLIVL_Pa'<PMX]bbTMQ[QZZM[Q[\QJTMè

#include <cstdlib>
#include <iostream> 
#include <fstream>

using namespace std;

int main(int argc, char *argv[]){
    
    cout << "Input Path: ";
    string inputPath = "";
    getline(cin, inputPath);
    ifstream plainText(inputPath.c_str());
    cout << '\n';
    
    cout << "Output Path: ";    
    string outputPath= "";
    getline(cin, outputPath);
    ofstream cipherText(outputPath.c_str());
    cout << '\n';
    
    cout << "Shift Value = ";
    int shiftValue = 0;
    cin >> shiftValue;
    cout << '\n';
    
    string plainLine = "";
    
    while (getline(plainText, plainLine)){
    
        getline(plainText, plainLine);
        int lineLength = 0;
        lineLength = plainLine.length();  
        
        for (int i=0; i<=lineLength; i++){
            
            int decimalValue[lineLength];
            char cipherLine[lineLength];
        
            decimalValue[i] = (int)plainLine[i] + shiftValue; 
            cipherLine[i] = (char)decimalValue[i];
            cipherText << cipherLine[i];  
            }   
  
        cipherText << endl;
        }

    system("PAUSE");
    return EXIT_SUCCESS;
    }

Recommended Answers

All 2 Replies

whele your problem is that when does this

while (getline(plainText, plainLine)){

thats where your first paragraph goes... change for

while (!plainText.eof())

and it will work

whele your problem is that when does this

thats where your first paragraph goes... change for

while (!plainText.eof())

and it will work

Thank you very much. It worked! : )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.