Rotor class reverses wierdly

P

Protoman

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.
 
J

Jim Langston

Protoman said:
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;

Didn't you actually mean
CurPos += 36;
here?
}
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top