For my program I am supposed to implement an encryption and decryption algorithm. I then have to print it out using my functions

I have to read the input from a file, “plaintext.txt” The file will contain plaintext strings that are all lowercase, and be of the format:
3 hello world
2 how are you
Each line contains an integer as the first thing in the line, representing the key, k, that is used in the encryption (and decryption) shift.
There will be no numbers or punctuation in the plaintext input string.

So far I created a function, parseString( ), that takes the inputString, and stores the key and the plaintext in the other two parameters. Now I have 2 functions for encryption and decryption. I am having trouble trying to do pass by reference. I have to pass the same string to decryption as I do to encryption. By doing that I could modify the string passed to the decryption function and get it back to the original plaintext by subtracting k characters. If I can fix this then I pretty sure my program will run. Don't worry about whether my void functions are calculating the encryption/decryption in the right way b/c its not 100 % complete yet. Could anyone please help me fix this problem?

This is my code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// inputString : the entire line you read from file
// plainText : a string reference, which will include everything except the integer in the front
// key : an int reference. This will be the key taken from the beginning of the line

void parseString(string inputString, string& plainText, int& key);
void encryption(string& plainText, int key);
void decryption(string& cipherText, int key);
int main()
ifstream infile;
string tempString;
string plaintext;
int key = 0;"plaintext.txt");

getline(infile, tempString, '\n'); //read line until newline is encountered
parseString(tempString, plaintext, key);
cout<<"Plaintext : "<<plaintext<<endl;
cout<<"Key : "<<key<<endl;
encryption(plaintext, key);
decryption(cipherText, key);

}//end while
return 0;

void parseString(string inputString, string& plainText, int& key)
int spaceIndex = 0;
string tempKey; //will hold the string version of the key before conversion

spaceIndex = inputString.find_first_of(" "); //finds the first whitespace
tempKey = inputString.substr(0,spaceIndex); //capture the data up to the first white space

key = atoi(tempKey.c_str()); //convert the char* equivalent of tempKey to integer (atoi = ASCII to int)
plainText = inputString.substr(spaceIndex+1, inputString.length()-spaceIndex);
void encryption(string& plainText, int key)
for (int i = 0; i < plainText.length(); ++i)
plainText[i] = plainText[i] + key;
cout << "Encrypted text is " << plainText << endl;
void decryption(string& cipherText, int key)
for (int i = 0; i < cipherText.length(); ++i)
cipherText[i] = cipherText[i] - key;
cout << "Decrypted text is " << cipherText << endl;

cipherText was not declared in main(). When I compiled this I had to add

string cipherText;

to main(). Your encryption and decryption functions are going to need some work because saying something like cipherText - key is not going to remove the int from the end of the string (If that is what you're trying to do.

This article has been dead for over six months. Start a new discussion instead.