 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Protoman Guest
|
Posted: Sat Dec 30, 2006 10:10 am Post subject: Rotor class reverses wierdly |
|
|
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,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",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. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|