Urgent Help with Encryption Program !

Hi ,
I am a student , I have an assignment due in Computer Science , I have to create a simple encryption program.
The main.cpp has to include a function.h file.

The Encryption method being used is the Caesar Cipher , which basically
shifts the letters of the message.
For instance "abcd" would encrypt into "defg" with 3 shifts.

But when I try this with my program I get the following in the console

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 matthew@matthew-VirtualBox:~$ cd Desktop/Prac5/Task2
matthew@matthew-VirtualBox:~/Desktop/Prac5/Task2$ make
g++ -c task2functions.cpp -o Task2functions.o
task2functions.cpp:2:18: warning: extra tokens at end of #include directive [enabled by default]
g++ -static Task2.o Task2functions.o -o Caesar.out 
matthew@matthew-VirtualBox:~/Desktop/Prac5/Task2$ ./Caesar.out
Enter the shift distance: 3
Enter a string to be encrypted: abcd
The original string was: abcd
The encrypted message is: [����h����{�����������������������#����M����Z����{������������������y��������������E����_����������������������������
         ��������0����\����l��������� ����@����M����g������������������������!�������d@@88@
                                   �
��]>>���������)����y/�h8d�$�3��h
x86_64Segmentation fault (core dumped)



This is the main.cpp ,
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
 #include <iostream>
#include <limits>
#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;
}


My .h File

1
2
3
4
5
6
#ifndef TASK2FUNCTIONS_H
#define TASK2FUNCTIONS_H
#include <string>
using namespace std;
string caesarCipher(const string& text, int shift);
#endif 


And the .h file's .cpp
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
29
#include "task2functions.h"
#include string ;
using namespace std;

string caesarCipher(const string& text,int shift)
{
	string Encrypt,output;
	char c ;
	{
		string Cipher = "";
		for(int a = 0 ; a < Encrypt.length();a++)
		{
			if ( isalpha(Encrypt[a]))
			{
				c = toupper(c);
				c = c + shift;
			}
			else
			{
				c = Encrypt[a];
			}
			Cipher += c;
		}
	}


}



I'm not sure exactly how to change a char using it's integer value.

My makefile , in case there is something wrong with it
1
2
3
4
5
6
7
8
9
10
Caesar.out: Task2.o Task2functions.o
	g++ -static Task2.o Task2functions.o -o Caesar.out 

Task2functions.o : task2functions.cpp
	g++ -c task2functions.cpp -o Task2functions.o

Task2.o : task2.cpp task2functions.h
	g++ -c task2.cpp -o Task2.o
	
	


Please , I need to finish this task within the next 16 hours !

So so overkill, look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

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

int main()
{

	string myString;

	// get the string
	cout << "Enter string: ";
	cin >> myString;

	// loop as per the length
	for (int i = 0; i < myString.length(); i++)
		myString[i] = myString[i] + 3;  // add 3 to char code

	cout << myString;


	return 0;
}
Enter string: abcd
defg
Feel really stupid right now , hahaha.

Thank you so much !

Nah, dont :) - all part of learning.
Topic archived. No new replies allowed.