I'm writing a program that simulates the Enigma machine. I'm basing it
around a class called Rotor. It has a ReverseRotor() fn that...reverse
the rotor by how many steps. But it works strangely. Compile and run
this:
#include <iostream>
#include <cstdlib>
using namespace std;
class Rotor
{
public:
Rotor():CurPos(0)
{
memcpy(Alphabet,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456 789",36);
}
void SetRotorPosition(int NewPos)
{
while(NewPos < 0)
{
NewPos +=36;
}
CurPos = NewPos%36;
}
void AdvanceRotor(int Steps)
{
CurPos += Steps;
while(CurPos < 0)
{
CurPos +=36;
}
CurPos %=36;
}
void ReverseRotor(int Steps)
{
CurPos -= Steps;
while(CurPos < 0)
{
CurPos-=36;
}
CurPos%=36;
}
char GetCurrentCharacter()
{
return Alphabet[CurPos];
}
char GetCharacterIndex(int Index)
{
return Alphabet[(CurPos+Index)%36];
}
private:
char Alphabet[36];
int CurPos;
};
int main()
{
Rotor rotor;
int index;
for(int i=0;i<36;i++)
{
cout << "Enter index: " << endl;
cin >index;
cout << "Character at index: " << rotor.GetCharacterIndex(index) <<
endl;
rotor.ReverseRotor(1);
}
system("PAUSE");
return EXIT_SUCCESS;
}
Enter 1 and you'll get B, as expected. Enter 1 again and you'll
get...E? Not A as I would expect. Something's wrong here, and I can't
figure it out. Any help here? Thanks!!!!
BTW, AdvanceRotor() works just fine.