By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

Rotor class reverses wierdly

P: n/a
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.

Dec 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Protoman" <Pr**********@gmail.comwrote in message
news:11*********************@k21g2000cwa.googlegro ups.com...
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;
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.

Dec 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.