0

When it gets to

        cout << "Chose direction of rotor 1:" << endl;
        cout << "*(i.e.:enter 1 for left and 2 for right)" << endl;

and i enter 1 or 2 it couts "Wrong choice"

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

//global varibals:
char alphabet_of_rotor1[26];
char alphabet_of_rotor2[26];
char rotor1_forward(char Letter);
char rotor1_backward(char Letter);
char rotor2_forward(char Letter2);
char rotor2_backward(char Letter2);
char print(char print_char);

int main()
{
    cout << "This is enigma machine:" << endl;
    cout << "Enter letters for the first rotor:" << endl;
    cin >> alphabet_of_rotor1;
cout<<"--------------------------------------------------------------"<<endl;
    cout << "Enter letters for the second rotor:" << endl;
    cin >> alphabet_of_rotor2;
cout<<"--------------------------------------------------------------"<<endl;
    cout << "Enter letter to encode:" << endl;
    char Letter;
    cin >> Letter;
cout<<"--------------------------------------------------------------"<<endl;
    cout << "Chose direction of rotor 1:" << endl;
    cout << "*(i.e.:enter 1 for left and 2 for right)" << endl;
    int direction_of_rotor1;
    cin >> direction_of_rotor1;
    switch(direction_of_rotor1)
    {
        case 1:
cout<<"--------------------------------------------------------------"<<endl;
        rotor1_forward(Letter);
        case 2:
cout<<"--------------------------------------------------------------"<<endl;
        rotor1_backward(Letter);
        default:
cout<<"--------------------------------------------------------------"<<endl;
        cout << "Wrong choice" <<endl;
    }
    system("PAUSE");
    return 0;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
char rotor1_forward(char Letter)
{
       for (int alphabet1=0;alphabet1>26;alphabet1++)
       {
           if(alphabet_of_rotor1[alphabet1] == Letter)
           {
                    cout << "Chose direction of rotor 2:" << endl;
                    cout << "*(i.e.:enter 1 for left and 2 for right)" << endl;
                    int direction_of_rotor2;
                    cin >> direction_of_rotor2;
                    switch(direction_of_rotor2)
                    {
                    case 1:
                    cout<<"--------------------------------------------------------------"<<endl;
                    rotor2_forward(alphabet_of_rotor1[alphabet1-1]);
                    case 2:
                    cout<<"--------------------------------------------------------------"<<endl;
                    rotor2_backward(alphabet_of_rotor1[alphabet1-1]);
                    default:
                    cout<<"--------------------------------------------------------------"<<endl;
                    cout << "Wrong choice 2" <<endl;
                    }
           }
       }
    return 0;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
char rotor1_backward(char Letter)
{
       for (int alphabet1=0;alphabet1>26;alphabet1++)
       {
           if(alphabet_of_rotor1[alphabet1] == Letter)
           {
                    cout << "Chose direction of rotor 2:" << endl;
                    cout << "*(i.e.:enter 1 for left and 2 for right)" << endl;
                    int direction_of_rotor2;
                    cin >> direction_of_rotor2;
                    switch(direction_of_rotor2)
                    {
                    case 1:
                    cout<<"--------------------------------------------------------------"<<endl;
                    rotor2_forward(alphabet_of_rotor1[alphabet1+1]);
                    case 2:
                    cout<<"--------------------------------------------------------------"<<endl;
                    rotor2_backward(alphabet_of_rotor1[alphabet1+1]);
                    default:
                    cout<<"--------------------------------------------------------------"<<endl;
                    cout << "Wrong choice 2" <<endl;
                    }
           }
       }
    return 0;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
char rotor2_forward(char Letter2)
{
       for (int alphabet2=0;alphabet2>26;alphabet2++)
       {
           if(alphabet_of_rotor2[alphabet2] == Letter2)
           {
                print(alphabet_of_rotor2[alphabet2-1]);
           }
       }
    return 0;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
char rotor2_backward(char Letter2)
{
       for (int alphabet2=0;alphabet2>26;alphabet2++)
       {
           if(alphabet_of_rotor2[alphabet2] == Letter2)
           {
                print(alphabet_of_rotor2[alphabet2+1]);
           }
       }
    return 0;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
char print(char print_char)
{
    cout << print_char << endl;
    system("PAUSE");
    return 0;
}
2
Contributors
1
Reply
4
Views
4 Years
Discussion Span
Last Post by Schol-R-LEA
0

EDIT: I originally thought that this was a different problem, but then I noticed that you were, in fact, reading in a number and not a character. My error.

Looking over the code again, it's simply that you aren't putting break; statements at the end of each case of the switch().

Edited by Schol-R-LEA

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.