Member Avatar for 111100/11000

How to go back to

start_new_input:

of main() function?

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

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

int main()
{
    cout << "This is enigma machine:" << endl;
    cout << "*NOTE:This programe accepts only lower case letters" << endl;
cout<<"--------------------------------------------------------------"<<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 << "Chose direction of rotor 1:" << endl;
    cout << "*(i.e.:enter 1 for left and 2 for right)" << endl;
    cin >> direction_of_rotor1;
    cout<<"--------------------------------------------------------------"<<endl;
    int going_through_entered_characters_array=-1;
    cout << "Enter letter to encode:" << endl;
    cin >> entered_characters;
    start_new_input:  /* null statement required *///don't remove these 3 signs!!!
    char Letter;
    going_through_entered_characters_array++;
    Letter = entered_characters[going_through_entered_characters_array];
    if(entered_characters[going_through_entered_characters_array] != '\0')
    {
        switch(direction_of_rotor1)
        {
            case 1:
            cout<<"--------------------------------------------------------------"<<endl;
            rotor1_backward(Letter);
            break;
            case 2:
            cout<<"--------------------------------------------------------------"<<endl;
            rotor1_forward(Letter);
            break;
            default:
            cout<<"--------------------------------------------------------------"<<endl;
            cout << "Wrong choice" <<endl;
            break;
        }
    }
    else
    {
        cout << "\nend of program...";
        system("PAUSE");
    }
    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;
                    cin >> direction_of_rotor2;
                    switch(direction_of_rotor2)
                    {
                    case 1:
                    cout<<"--------------------------------------------------------------"<<endl;
                    rotor2_backward(alphabet_of_rotor1[alphabet1-1]);
                    case 2:
                    cout<<"--------------------------------------------------------------"<<endl;
                    rotor2_forward(alphabet_of_rotor1[alphabet1-1]);
                    default:
                    cout<<"--------------------------------------------------------------"<<endl;
                    cout << "Wrong choice 2.1" <<endl;
                    }
           }
       }
    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;
                    cin >> direction_of_rotor2;
                    switch(direction_of_rotor2)
                    {
                    case 1:
                    cout<<"--------------------------------------------------------------"<<endl;
                    rotor2_backward(alphabet_of_rotor1[alphabet1+1]);
                    break;
                    case 2:
                    cout<<"--------------------------------------------------------------"<<endl;
                    rotor2_forward(alphabet_of_rotor1[alphabet1+1]);
                    break;
                    default:
                    cout<<"--------------------------------------------------------------"<<endl;
                    cout << "Wrong choice 2.2" <<endl;
                    break;
                    }
                    break;
           }
       }
    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 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 print(char print_char)
{
    cout << print_char << endl;
    goto start_new_input;
    return 0;
}

You don't need to use a goto here. You should remove it from your program. I think, with a bit of tweaking, you can replace the goto with a while loop.

I think that labels have the same kind of scoping rules as other variables and functions in C++, so you can't goto a label in another function. As I said though, you don't need goto here, so I'd go for a simple tweaking to avoid it.

In the words of Dijkstra famously said:

The quality of programmers is a decreasing function of the density of go to statements in the programs they produce

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.