For my program I am supposed to implement an encryption and decryption algorithm.

You will 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. The problem is trying to implement the encryption and decryption functions. Can anyone please help me try to fix my functions for encryption and decryption? Thanks
This is my code so far:

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

//parseString
//INPUT:
// inputString : the entire line you read from file
//OUTPUT:
// 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 plainText, int key);

int main()
{
    ifstream infile;
    string tempString;
    string plaintext;
    int key = 0;
    infile.open("plaintext.txt");

    while(!infile.eof())
    {
        getline(infile, tempString, '\n');  //read line until newline is encountered
        parseString(tempString, plaintext, key);
        cout<<"Plaintext : "<<plaintext<<endl;
        cout<<"Key       : "<<key<<endl;

    }//end while
encryption(plaintext, key);
decryption(plaintext, key);
    infile.close();
    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)
{
string temp;
    for(int i=0; i <plainText.length();i++)
    {

      temp= plainText[i] + key;

    }

cout << "Encrypted text is " << temp << endl;
}
void decryption(string plainText, int key)
{
string temp2;
    for (int i = 0; i < plainText.length(); ++i)
    {
      temp2= plainText[i] - key;

        }
cout << "Decrypted text is " << temp2 << endl;
}

Once you encrypt the string, what are you supposed to do with it? Right now, I'm not exactly sure what you're doing, but it's not generating an encrypted string.

After I encrypt the string for the first line "hello world" and the string for the second line "how are you", I am supposed to print it out.
Obviously when I print it out it, it should look like "khoor zruog" and "jqy ctg aqw".
I think the logic of my function is wrong and I was wondering if you could help me fix it? I will continue to work on it though.

I think you should set your functions up to return strings. Then, inside the functions, you would build the new strings and return the new strings to the calling code.

Thanks for the advice. My output is looking much better but still looks incorrect. My encryption function was able to change the letters for the plaintext but the only problem is it replaces the spaces with some weird symbols. My decryption function was totally screwed b/c it returns completely different letters. Any idea on how to fix these mistakes. I will look at it myself and tell you if I make any progress.

Here is my code again with the corrections I made:

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

//parseString
//INPUT:
// inputString : the entire line you read from file
//OUTPUT:
// 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);
string encryption(string plainText, int key);
string decryption(string plainText, int key);

int main()
{
    ifstream infile;
    string tempString;
    string plaintext;
    int key = 0;
    infile.open("plaintext.txt");

    while(!infile.eof())
    {
        getline(infile, tempString, '\n');  //read line until newline is encountered
        parseString(tempString, plaintext, key);
        cout<<"Plaintext : "<<plaintext<<endl;
        cout<<"Key       : "<<key<<endl;
    cout<<"The encryption of the text is "<<encryption(plaintext, key)<<endl;
cout<<"The decryption of the text is "<<decryption(plaintext, key)<<endl;
    }//end while
    infile.close();
    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);
}
string encryption(string plainText, int key)
{
string temp;
    for(int i=0; i <plainText.length();i++)
    {

      temp+= plainText[i] + key;

    }

return temp;
}
string decryption(string plainText, int key)
{
string temp2;
    for (int i = 0; i < plainText.length(); i++)
    {
      temp2+= plainText[i] - key;

        }
return temp2;
}

Please start using [code] ...code tags... [/code] when you post code.

#include <iostream>
int main() {
  std::cout << "This looks nice, and is easily readable." << std::endl;
  return 0;
}

#include <iostream>
int main() {
std::cout << "This looks like dung, and isn't readable." << std::endl;
return 0;
}

Your code is very difficult to read, I'm afraid I can't help at the moment. You still have edit time, add your tags and I'll try to take another look.

OK, here is my code with the CODE tags.

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

//parseString
//INPUT:
// inputString : the entire line you read from file
//OUTPUT:
// 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 plainText, int key);

int main()
{
ifstream infile;
string tempString;
string plaintext;
int key = 0;
infile.open("plaintext.txt");

while(!infile.eof())
{
getline(infile, tempString, '\n'); //read line until newline is encountered
parseString(tempString, plaintext, key);
cout<<"Plaintext : "<<plaintext<<endl;
cout<<"Key : "<<key<<endl;

}//end while
encryption(plaintext, key);
decryption(plaintext, key);
infile.close();
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)
{
string temp;
for(int i=0; i <plainText.length();i++)
{

temp= plainText[i] + key;

}

cout << "Encrypted text is " << temp << endl;
}
void decryption(string plainText, int key)
{
string temp2;
for (int i = 0; i < plainText.length(); ++i)
{
temp2= plainText[i] - key;

}
cout << "Decrypted text is " << temp2 << endl;
}

Sorry, I posted the wrong code. That was my old code. This is my correct code.

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

//parseString
//INPUT:
// inputString : the entire line you read from file
//OUTPUT:
// 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);
string encryption(string plainText, int key);
string decryption(string plainText, int key);

int main()
{
	ifstream infile;
	string tempString;
	string plaintext;
	int key = 0;
	infile.open("plaintext.txt");

	while(!infile.eof())
	{
		getline(infile, tempString, '\n');  //read line until newline is encountered
		parseString(tempString, plaintext, key);
		cout<<"Plaintext : "<<plaintext<<endl;
		cout<<"Key       : "<<key<<endl;
	cout<<"The encryption of the text is "<<encryption(plaintext, key)<<endl;
cout<<"The decryption of the text is "<<decryption(plaintext, key)<<endl;
	}//end while
	infile.close();
	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);
}
string encryption(string plainText, int key)
{
string temp;
	for(int i=0; i <plainText.length();i++)
	{
	temp+= plainText[i] + key;
	}
	
	
return temp;
}
string decryption(string plainText, int key)
{
string temp2;
for (int i = 0; i < plainText.length(); ++i)
{
temp2+= plainText[i] - key;

}
return temp2;
}
string encryption(string plainText, int key)

{

string temp;

for(int i=0; i <plainText.length();i++)
{
[COLOR="Red"]temp+=[/COLOR] plainText[i] + key;
}
return temp;

}

Maybe changing temp+= plainText[i] + key; to temp[i] = plainText[i] + key; would work... The same logic goes with the decryption one.

Okay, I modified my code a bit and this is what I have so far. Please bare with me, I am a beginner in C++.
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.

I then have to come up with 2 functions to calculate encryption and decryption.
This should be the correct output for the program:

Plaintext : hello world
Key : 3
Encyrypted text is khoor zruog
Decrypted text is hello world

Plaintext: how are you
Key : 2
Encrypted text is jqy ctg aqw
Decrypted text is how are you

Can anyone help me fix this program? I have been struggling with it for days.

Here is my code

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

/*parseString
INPUT:
inputString : the entire line you read from file
OUTPUT:
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;
    string cipherText;
    int key = 0;
    infile.open("plaintext.txt");

    while (!infile.eof()) {
	getline(infile, tempString); /**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**/
    infile.close();
    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;
}
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.