Object oriented Function call

Hi I have a small problem, I've a code for "caesar cipher" which is functioning properly in c++ (no logic problem) in a single file...I tried to implement it using object oriented programming and separate the code in 3 separate files (Test(main), header, .cpp)

But i think there is a problem with my function call...For example when I type in "abc" and the shifting is 1 its supposed to give "bcd" but its giving me "abc"...Could you guys help...I'm quite new to object oriented...Could you guys please be clear and precise when you comment so that I understand what's the problem...

The main
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
30
31
  #include<iostream>
#include<string>
#include "CyclicShift.h"

using namespace std;


int main()
{

	string text;//the string that holds the user input

	int key;//key holds the number by which the user wants the alphabets to be shifted

	cout << "Enter your phrase: " << endl;
	getline(cin, text);//gets the user input ( including spaces and saves it to the variable text)

	cout << "Please choose a number(key) for which you wants the alphabets to be shifted:  " << endl;
	/*Note: the key can either be positive (forward shifting), negative (backward shifting) or zero (no shifting)*/

	cin >> key;//User input for number by which alphabets are going to be shifted

	const CyclicShift aShift;
	
	cout << "Encrypted Message : " << aShift.Encrypt(text, key) << endl;

	//system("Pause");

	return 0;
}


The header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include <iostream>
#include<string>
//Note: No using "namespace std;" in header files 

class CyclicShift
{
private:
	char fUpperCase[26];//A-Z
	char fLowerCase[26];//a-z

public:
	CyclicShift();

	std::string& Encrypt(std::string& aOriginalMessage, int &aKey) const;// Function Prototype.  This declares Encrypt to be a function that needs one string and one integer variables as arguments. Reference operator & in prototype
	//string Decrypt(string&, int &);// Function Prototype.  This declares Decryptto be a function that needs one string and one integer variables as arguments. Reference operator & in prototype
	//void display();
};


.cpp file
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "CyclicShift.h"
#include<iostream>
#include<string>

using namespace std;

CyclicShift::CyclicShift()
{
	char fUpperCase[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};//Initialization of class member
	char fLowerCase[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};//Initialization of class member

}

string& CyclicShift::Encrypt(string& aOriginalMessage, int & aKey) const
{

	int z;//z holds the value (int) of the length of the user input ( including spaces)
	z = (int)aOriginalMessage.length();	/*give the variable z the value of the user input length and length is normally an unsigned long integer hence
								we have to typecast it*/

	/*counter that makes it keep looping until it "encrypts" all of the user input (that's why it keeps looping while its less than z)*/
	for (int i = 0; i < z; i++)
	{
		for (int j = 0; j < 26; j++)
		{
			if (aOriginalMessage[i] == fLowerCase[j] || aOriginalMessage[i] == fUpperCase[j])
			{

				if (aKey > 0)
				{
					//another counter that loops forwards key times by incrementing method
					for (int counter = 0; counter < aKey; counter++)
					{
						/*it checks if the letter text[x] is 'z' and if it is 'z' it will make it 'a'*/
						if (aOriginalMessage[i] == 'z')
						{
							aOriginalMessage[i] = 'a';
						}

						else if (aOriginalMessage[i] == 'Z')
						{
							aOriginalMessage[i] = 'A';
						}

						else
						{
							aOriginalMessage[i]++;
						}

					}
				}

				else	if (aKey < 0)
				{
					//another counter that loops backwards key times by decrementing method
					for (int counter = 0; counter < abs(aKey); counter++)
					{
						/*it checks if the letter text[x] is 'a' and if it is 'a' it will make it 'z'*/
						if (aOriginalMessage[i] == 'a')
						{
							aOriginalMessage[i] = 'z';
						}

						else if (aOriginalMessage[i] == 'A')
						{
							aOriginalMessage[i] = 'Z';
						}

						else
						{
							aOriginalMessage[i]--;
						}
					}
				}

				else
				{
					aOriginalMessage[i];//No alphabet shifts
				}

			}

			else {

				continue;
				}

			break;
		}

	}

	return aOriginalMessage;
}

Last edited on
Please help needed guys...It's a bit urgent
Topic archived. No new replies allowed.