Rotor class reverses wierdly

Discussion in 'C++' started by Protoman, Dec 30, 2006.

  1. Protoman

    Protoman Guest

    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.
     
    Protoman, Dec 30, 2006
    #1
    1. Advertising

  2. Protoman

    Jim Langston Guest

    "Protoman" <> wrote in message
    news:...
    > 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.
    >
     
    Jim Langston, Dec 30, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robin Becker

    rotor alternative?

    Robin Becker, Nov 18, 2003, in forum: Python
    Replies:
    60
    Views:
    1,556
    John J. Lee
    Nov 25, 2003
  2. rf
    Replies:
    3
    Views:
    446
    Adrienne Boswell
    Sep 9, 2010
  3. richard

    Re: tables behave wierdly

    richard, Sep 9, 2010, in forum: HTML
    Replies:
    0
    Views:
    382
    richard
    Sep 9, 2010
  4. Ceriousmall
    Replies:
    7
    Views:
    419
    Lew Pitcher
    Mar 29, 2011
  5. Frank Spade

    IE reverses logic

    Frank Spade, Jan 11, 2005, in forum: Javascript
    Replies:
    11
    Views:
    156
    Richard Cornford
    Jan 13, 2005
Loading...

Share This Page