Member Avatar for Member #958271
#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;


    //***i need to return to here and not to the beginning of main function***


    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;                //i intend to move these 
                    cout << "*(i.e.:enter 1 for left and 2 for right)" << endl;   //lines to main so
                    cin >> direction_of_rotor2;                                   //program wouldn't ask
                    switch(direction_of_rotor2)                                   //direction of rotor 2
                    {                                                             //on every letter
                    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;
                    }
           }
       }
    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;
                    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;
    //***i need to get from this line of this function here back to main***
    return 0;
}

Dani AI

Generated

Short answer: don’t try to “jump” back into the middle of main. Restructure the repeating work into a loop or a small function and call that from main. ’s suggestion to move the per-character processing into its own function/loop is the right design; ’s pointer about using break is useful for exiting inner loops, but it’s not a substitute for clearer flow.

Concrete refactor plan you can apply right away:

  • read rotor configurations and any per-run choices once (keep these out of the per-character code),
  • read the input into a std::string (avoid a fixed C buffer),
  • move the per-character logic into a function that takes the input char, rotor alphabets and directions, and returns the encoded char (do not prompt for input inside this function),
  • have main loop over the string, call that function for each character, and accumulate/print the output.

Small example of the intended structure (illustrative only):

char applyRotors(char c, int dir1, int dir2,
                 const std::string &r1, const std::string &r2);

std::string output;
for (size_t i = 0; i < input.size(); ++i) {
    output.push_back(applyRotors(input[i], dir1, dir2, rotor1, rotor2));
}

Key safety and correctness tips:

  • never index alphabet[index-1] or alphabet[index+1] without bounds checks; use modular arithmetic for wrap-around (prev = (idx + 25) % 26, next = (idx + 1) % 26).
  • functions declared to return char should actually return the computed character (or be void and let main handle output).
  • avoid globals where possible; pass state into functions.
  • ensure every case that should not fall through has a break.
  • prefer std::string/size_t and std::getline for robust input; system("PAUSE") is Windows-specific.

With those changes you get predictable control flow, no mysterious “jump back” behavior, and far easier testing and debugging.

Recommended Answers

All 2 Replies

Instead of trying to get back to the middle of a function why dont you just break it up into multiple functions to begin with? Something like:

int main () {
   /*
      do all output thing here as you already have
   */
   cout << "This is enigma machine:" << endl;
   // ...

   call_main_loop ();
   return 0;
}

void call_main_loop () {

   /* 
      Starting from where you'd like to loop back to
   */
   char Letter;
   going_through_entered_characters_array++;
   // ...
}

That way, to get back to that point, you just call call_main_loop.

commented: Thnx it worked +0

Where you call print() you can break out of the loop.

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.