Hello, I'm writing a simple shift cipher which reads from plain.txt, encrypts, and writes to cipher.txt. It works, but only for small plain text lines of a couple of words or so. If the plain text lines are long, then the program does not respond (freezes). Any help would be appreciated. Thanks.

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

using namespace std;

int main(int argc, char *argv[]){
    
    string line = "";
    int lineLength = 0;
    int shiftValue = 0;
    int asciiValue[lineLength];
    char cipherLine[lineLength];
    ifstream plainText ("plain.txt");
    ofstream cipherText ("cipher.txt");
   
    cout << "Please enter a shift value: ";
    cin >> shiftValue;
 
    while (!plainText.eof()){
    
    getline (plainText, line);
    lineLength = line.length() - 1;  
    
    for (int i=0; i<=lineLength; i++){
        asciiValue[i] = (int)line[i]; 
        asciiValue[i] += shiftValue;
        cipherLine[i] = (char)asciiValue[i];
        cipherText << cipherLine[i];  
        }   
        
    cipherText << endl << endl;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Works fine for me. Can you provide how and with what you tested it then it's freezes?

I'm using BloodShed for a compiler. My operating system is Vista 32bit.

Hey guys can you please try to encrypt this plain text and tell me what happens, it doesn't work for me:

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.

Edited 7 Years Ago by BobRoss: n/a

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

using namespace std;

int main(int argc, char *argv[]){
    
    string line = "";
    int lineLength = 0;
    int shiftValue = 0;
    int asciiValue[lineLength]; // <- Problem1
    char cipherLine[lineLength]; // <- Problem 2
    ifstream plainText ("plain.txt");
    ofstream cipherText ("cipher.txt");
   
    cout << "Please enter a shift value: ";
    cin >> shiftValue;
 
    while (!plainText.eof()){
    
    getline (plainText, line);
    lineLength = line.length() - 1;  
     // <- To add, Marker 1->
    for (int i=0; i<=lineLength; i++){
        asciiValue[i] = (int)line[i]; 
        asciiValue[i] += shiftValue;
        cipherLine[i] = (char)asciiValue[i];
        cipherText << cipherLine[i];  
        }   
        // <- To add, Marker 2 ->
    cipherText << endl << endl;
    }
    
    system("PAUSE"); // <- Warning 1
    return EXIT_SUCCESS;
}

Okay, you've got some problems with your code, as I have highlighted... I'll refer to the lines with the markers I have added...

1. & 2. You do realize that the maximum size of asciiValue and cipherLine is 0 => Its as good as initializing them to be normal variables, not as arrays, which you need here...
Solution: Initialize them as pointer-variables, so line 12 becomes:

int *asciiValue;

, and line 13 becomes:

char *cipherLine;

Marker 1. Here, you'll have to initialize the pointers mentioned above, so that they'll be the same size as the current line, not 0, so, add these two lines here:

asciiValue = new int[lineLength+1];
cipherLine = new char[lineLength+1];

Marker 2. Now, you'll have to deallocate the memory occupied by asciiValue and cipherLine , and keep adjusting its size according to the current length of the line, so, add this here:

delete cipherLine;
delete asciiValue;

Warning 1: Don't use system("pause"); ... use cin.get(); instead... See this for more info...

Hope this helped!

Edited 7 Years Ago by amrith92: n/a

amrith92, thank you very much. Everything works as intended now. I just have one more question:

1.
asciiValue = new int[lineLength+1];
2.
cipherLine = new char[lineLength+1];

Why do we need to add 1 to lineLength?

>> Why do we need to add 1 to lineLength?

That's because when we declare an array, say int arr[5]; , we are allocating, effectively 5*2 = 10 bytes of memory for it. Now, in your program, lineLength = line.length()-1; , which means that if line.length() = 5, and you need an integer array capable of holding 5 integer values(which will occupy 10 bytes), so you'd be expected to allocate that amount of memory for the array, arr . Now seeing as lineLength = 4, if line.length() = 5, you'll only be able to allocate 4*2 = 8 bytes of memory for it, so we're two bytes (i.e one integer value) short of what the program expects as the size of array arr . This becomes a problem when we run the loop, and try and store 5 integer values into arr , which can handle only 4 values, which ultimately results in a memory leak of sorts(when trying to deallocate the memory), and the program will complain when you run it...

Hope this clears it up... :)

Of course, if you'd like, you can just use this:

asciiValue = new int[line.length()];
cipherLine = new char[line.length()];

HeBa_4, you do realise you replied to a thread that was last posted to six years ago, don't you? I might be wrong, but I reckon there's as much chance of Donald Trump turning out to be the greatest President the USA has ever seen as there is of amrith92 providing the 'full code' for you... Just sayin' ;-)

This question has already been answered. Start a new discussion instead.