Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (ketz):

I have a Caesar cipher code in object oriented programming...but I don't know why it is not working...The input of the user should be encrypted but unfortunately it is not being encrypted...@Kainui

OpenStudy (ketz):

I have 3 attached files: The main, the header and the cpp

OpenStudy (ketz):

I'm getting the same message as the input message by user!

OpenStudy (kainui):

Sorry I don't know how to help you on this one. @ganeshie8 @wio maybe they can help.

OpenStudy (ketz):

ok

OpenStudy (ketz):

Hi, I need your help @ganeshie8

OpenStudy (ketz):

I need help @rishavraj

rishavraj (rishavraj):

@hartnn ur help is required :))

hartnn (hartnn):

the logic is fine... maybe some error in function calls or something... getting any compilation warning or error? or getting same output as input? how are you compiling/executing? online?

hartnn (hartnn):

you may try debugging by putting cout statements at different points on the code.

OpenStudy (ketz):

I'm using visual studio

OpenStudy (ketz):

It is compiling but I my message is not being encrypted...

OpenStudy (ketz):

Can you try the program...

OpenStudy (ketz):

I could not file any compiler online for object oriented programming that's why I posted it in this format...

OpenStudy (ketz):

when i input "abc" I get "abc"...no encryption!!!

OpenStudy (rsmith6559):

A couple of things: I don't see an object of CyclicShift instantiated, and nothing calls CyclicShift::Encrypt. So text is never changed.

OpenStudy (ketz):

Could you please type the line of code that I need to change for my program to work.....

OpenStudy (ketz):

I'm quite new to object oriented.....

OpenStudy (ketz):

Need more precision @rsmith6559

OpenStudy (rsmith6559):

In OOP, a class describes a class of object. It doesn't create any objects, it just describes the object[s] that you can instantiate from this class. In C++ you instaniate an object with: CyclicShift foo = new CyclicShift(); Calling the member function Encrypt would be done: string bar = foo.Encrypt( "abc", key ); In many other languages member functions are called methods.

OpenStudy (ketz):

but I already created an object called aShift...but it was not created dynamically...

OpenStudy (ketz):

!!! @rsmith6559

OpenStudy (rsmith6559):

I have no idea what this line actually does: const CyclicShift aShift; It may create an object, although I would expect the const to make the instance variables immutable (changeable). I've always dynamically allocated objects.

OpenStudy (ketz):

even if I remove the const the output of the code is still the same...

OpenStudy (ketz):

I made the changes you told me...it still does not work!!! @rsmith6559

OpenStudy (rsmith6559):

Are you getting a clean compile? C++ is usually rather fussy, and the stuff we've been going after should tank the compiler. If you're using g++, use the -Wall switch to flag all warnings.

OpenStudy (lyrae):

It does compile after some minor file renaming and cleanup of the headers. I was also able to replicate your result when running it and it does not encrypt. gcc on Ubuntu: ``` g++ -std=c++11 -Wall -o cesar CyclicShift.cpp CaesarCipher.cpp -lm ^ ^ Initializer lists are only avalible in > c++11 Tell linker to link with math library ``` The problem is in the CyclicShift contructor. It causes ``` char fUpperCase[26];//A-Z char fLowerCase[26];//a-z ``` to be initialized with junk, which in turn results in an important if statement block newer (very rarely) getting executed. See if you can find the problem using the hints :) Regarding ``` const CyclicShift aShift; ``` it creates a local variable and implicitly calls the default constructor of CyclicShift. The difference between this and ``` const CyclicShift *aShift = new CyclicShift(); ``` Is that the first one lives as long as the local variable but the second one explicitly has to be deleted or otherwise you have a memory leak. I try to avoid using the terms stack and heap in c++ as they are a lite more ambiguous and some people find the use of them controversial. The *const* modifier will indeed cause the compiler to trow errors if a member is modified or method without *const* in the end is called. If the method has *const* in the end it will access the object using *const this* and is hence not allowed to modify a member not explicitly declared as mutable.

OpenStudy (lyrae):

Correction: "...block newer..." = block never Another observation is that because aShift is const it is probably put in BSS section and initialized to all '\0' which still causes the same problem as if const is removed (initialized with junk).

OpenStudy (rsmith6559):

The constructor looks like it inits fUpper and fLowerCase properly. The file naming is off, so it's fun to find things. The filenames should be: main.cpp // currently main.h CyclicShift.h // currently caesarcipherheaderfile.c CyclicShift.cpp // currently caesarciphercppfile.h

OpenStudy (lyrae):

Well, it's always good to follow naming convention although c/c++ compilers usually are a bit more forgiving with this as long as you do proper includes and use the correct command line arguments unlike for example java which actually require certain naming to function properly. I was a bit unclear in my previous post, I got everything to compile w/o warnings or errors, ran the program and got the same result as @ketz ie. no encryption. After that I read the code and noticed a error in the constructor, fixed that error, and now everything works properly. The problem causes ``` CyclicShift::fUpperCase // and CyclicShift::fLowerCase ``` to still be uninitialized, which in turn always makes this if statement fail ``` if (aOriginalMessage[i] == fLowerCase[j] || aOriginalMessage[i] == fUpperCase[j]) ``` and no encryption of any character is performed. Example: http://ideone.com/bDq9LQ

OpenStudy (rsmith6559):

It took me a couple of hours, but I got @Lyrae code fixed so that it worked for me. What turned out to be the problem is that once the data type is declared in the class definition, if it's declared again in the constructor, it basically makes a variable local to the constructor. Just deleted the int s in the constructor definition and it works fine. Unfortunately, because of the array semantics I didn't get that to graft and compile on @ketz code.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!