Hi guys! I haven't been visiting this forum for nearly three years.
I was browsing some of my old programs, and I came across an Enigma rotor cipher simulator I coded. I re-compiled it, and now I have a runtime error I never had before:
It'll ask me whether I want to encrypt or decrypt, and let me enter the rotor settings, but then it won't let me enter the cleartext.
The code is attached. I hope I followed proper OO principles! It's probably some very simple error in main()...I hope.
Thanks!
Last edited by Apollyon; July 2nd, 2010 at 09:39 PM.
The nerds will rule over the common folk. The geeks will rule over the nerds. The supergeeks will rule over the geeks. The hackers will rule over the supergeeks. The superhackers will rule over the hackers. Mwhahaha; I'll rule you all!!!
Second, your Rotor class is faulty. It has a pointer as a member variable, and this pointer points to dynamically allocated memory. However, your Rotor class has no user-defined copy constructor or assignment operator.
So I stopped right there. There is no need to go any further once a program uses classes coded this way.
The same thing with the Enigma class. It isn't safely copyable or assignable. Also, why are you using "goto"? That alone could be an automatic candidate for disqualification for anyone looking at your code.
You included <string>, but you make no use of the std::string class. Doing that would alleviate the problems I pointed out. There is no "weirdness", as your thread title suggests, when you have code like this. It just isn't coded correctly and safely.
OK, I fixed all those problems (I wrote a cctor and assignment operator for both classes), and yes, I am using the string class, despite you saying I hadn't: look in the Enigma and main files.
I also indented the code so it's easier to read.
And, I got it to work!!! I needed to read up on C++ I/O streams.
Here's the new code.
The nerds will rule over the common folk. The geeks will rule over the nerds. The supergeeks will rule over the geeks. The hackers will rule over the supergeeks. The superhackers will rule over the hackers. Mwhahaha; I'll rule you all!!!
OK, I fixed all those problems (I wrote a cctor and assignment operator for both classes), and yes, I am using the string class, despite you saying I hadn't:
So why didn't you use the same std::string class in your Enigma and Rotor classes instead of char*? Then you wouldn't need to write copy constructor and assignment operator -- that's the real point I was trying to make.
Secondly, you did not actually write any implementation for the copy constructor or assignment operator.
Code:
#include <iostream>
#include <string>
using namespace std;
class Rotor
{
public:
Rotor(char pos);
int GetSteps()const{return steps;}
static int CharacterMap(char the_char);
void SetRotorPosition(int NewPos);
void AdvanceRotor(int Steps);
void ReverseRotor(int Steps);
char GetCurrentCharacter()const;
char GetCharacterIndex(int Index)const;
char GetCharacterInverse(int i)const;
private:
std::string Alphabet;
int CurPos;
int steps;
};
So why didn't you use the same std::string class in your Enigma and Rotor classes instead of char*? Then you wouldn't need to write copy constructor and assignment operator -- that's the real point I was trying to make.
Secondly, you did not actually write any implementation for the copy constructor or assignment operator.
Code:
#include <iostream>
#include <string>
using namespace std;
class Rotor
{
public:
Rotor(char pos);
int GetSteps()const{return steps;}
static int CharacterMap(char the_char);
void SetRotorPosition(int NewPos);
void AdvanceRotor(int Steps);
void ReverseRotor(int Steps);
char GetCurrentCharacter()const;
char GetCharacterIndex(int Index)const;
char GetCharacterInverse(int i)const;
private:
std::string Alphabet;
int CurPos;
int steps;
};
I made your requested changes. I also removed the Encrypt function, and overloaded the function call operator to do it's job. To make the code neater.
Also, did I follow proper OOP principles in designing my classes? I'd hate to see this written without using OOP; it'd likely be really long.
And since Rotors are only useful in Enigmas, should I make Rotor a private nested class of Enigma, so a Rotor cannot be instantiated out of context?
Anyway, thanks! Your advice was very helpful.
Last edited by Apollyon; July 3rd, 2010 at 08:37 PM.
The nerds will rule over the common folk. The geeks will rule over the nerds. The supergeeks will rule over the geeks. The hackers will rule over the supergeeks. The superhackers will rule over the hackers. Mwhahaha; I'll rule you all!!!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.