Problem with Encryption Program.

Hi , I posted on the beginner's forum Earlier.
Even though it's a simple program , the assignment requires
the encryption to be done by an external function.
Basically the program receives an input , and encrypts it by
shifting the alphabetical letters by a desired amount. when the shift goes past Z
it should start over at the beginning of the alphabet.
Example : Encrypting "abcd" with a shift of 3 would output "defg"

my program returns a blank value.
1
2
3
4
5
Enter the shift distance: 3
Enter a string to be encrypted: abcd
The original string was: abcd
The encrypted message is: 
Decryption of the encrypted message: 


Here is the main.cpp which we received pre-coded.
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
#include <iostream>
#include <limits>
#include <string>
#include "task2functions.h"

using namespace std;

int main() 
{
	int shift;
	cout << "Enter the shift distance: ";
	cin >> shift;
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	string testString;
	cout << "Enter a string to be encrypted: ";
	getline(cin,testString,'\n'); 
	
	cout << "The original string was: " << testString << endl;
	string encrypted;
	encrypted = caesarCipher(testString, shift);
	cout << "The encrypted message is: " << encrypted << endl;
	cout << "Decryption of the encrypted message: ";
	cout << caesarCipher(encrypted, -1 * shift) << endl;
	return 0;
}


The function.cpp , which is where I have to our programming
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
#include "task2functions.h"
#include <cstring> 
#include <cctype>
#include <string>
using namespace std;

string caesarCipher(const string& text,int shift)  // DO NOT CHANGE HEADER
{
	int x;
	string Encrypt;
	
	for (x=0; x < text.length(); x++)
	{
		toupper(text[x]);
		
		if (text[x] >= '!' && text[i] <= '@' || text[x] >= '[' && text[x] <= '}')
			Encrypt[x] = text[x];
		
		else if (text[x] + shift > 'Z')
			Encrypt[x] = (text[x] -26) + shift;
		
		else Encrypt[x] += shift;
		
		
	}
	return Encrypt ;
}
Last edited on
You are lucky it did not crash.
string Encrypt was default constructed and has a length of 0, so any subscript operation will be out of bounds and cause undefined behavior.
When you are trying to output that string, it sees that length is still 0 and do not output anything.

Also I would advise against things like text[x] >= '!'. You cannot know what codepage will be used on target PC. That is why you should use functions like isalpha instead of checking character code.
I'm still very new to c++ programming , so I'm not really sure what
you're telling me. How do I fix it ?

Sorry X(
just make Encrypt same size as text.
Either by initializating by copy: string Encrypt = text;
Creating a string of some size: string Encrypt(text.size());
Or by resizing it after declaration:
1
2
string Encrypt;
Encrypt.resize(text.size());


Here is a bonus — caesar cypher in C++11:
http://ideone.com/55hXhc
Last edited on
Well, maybe you can use Encrypt += text[x];
Okay Thank you, it's working. but after Encrypting it outputs the original input again.
Can't figure out what's wrong with my function ?
Show your current code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string caesarCipher(const string& text,int shift)  // DO NOT CHANGE HEADER
{
	int x;
	string Encrypt;
	
	for (x=0; x < text.length(); x++)//[Warning] comparison between signed and unsigned integer expressions [-Wsign-compare]
	{
		toupper(text[x]);
		
		if ((text[x] >= '!' && text[x] <= '@' )|| (text[x] >= '[' && text[x] <= '}'))//NOTE:priority!
			Encrypt += text[x];
		
		else if (text[x] + shift > 'Z')
			Encrypt += (text[x] -26) + shift;
		else if(text[x]+shift<'A')//shift maight be negative!
			 Encrypt += (text[x] +26) + shift;
		
		else Encrypt+=text[x]+shift;//Encrypt[x] += shift;
		
		
	}
	return Encrypt ;
}

Small letters are matched by (text[x] >= '[' && text[x] <= '}')) condition and are not encrypted at all.
Topic archived. No new replies allowed.