# Can I make a nested loop out of this?

• 02-15-2005
RpgActioN
Can I make a nested loop out of this?
Ok, I'm making a multipurpose crypto calc, and I need a brute forcer for all possible caesar ciphers... I know exactly how to do it, and I could finish the code right now, only that would make it extremely cumbersome....

Look at the bolded (if it doesn't bold, I am talking about the for loop):

Code:

```void caesar()          //Return all possibilities of caesar encryption outcomes {  char word[200];  int x, numchar1, numchar2, wordlength;  cout<<"Enter the word(s) you want to brute force with the Caesar Cipher in lower case." <<endl;  cin.getline(word, 200);  wordlength = strlen(word);  if(wordlength > 200)  {   cout<<"Your query was too long." <<endl;   caesar();  }  cout<<endl <<"Here is a list of possible solutions:" <<endl;  for(x = 0; x < wordlength; x++)  {   numchar1 = (int)word[x];   if(numchar1 == 122)          //if the character is z, reset it to a   numchar2 = 97;   else if(numchar1 == 32)      //space   numchar2 = 32;   else   numchar2 = numchar1 + 1;  //change to the next letter in the alphabet   cout<<char(numchar2);  }  cout<<endl; }```

The last time I encountered nested loops, I had an extremely hard time comprehending it, and here is no different. I know that I want the bolded to loop 26 times, BUT each time I want numchar2 to equal numchar1 + (2, 3, 4, so on...), and I want to subtract the first if statement by one (go to a if you're currently at y this time, and x the next time, and w the time after that). Is that possible with a nested loop or am I better off just copying and pasting that 26 times and making necessary adjustments?
• 02-15-2005
major_small
here, and some quick readability pointers:
Code:

```for(int i=0; i<26; i++) {     for(x = 0; x < wordlength; x++)     {         if(word[x] == 'z')      //why use hard-to decipher ASCII codes?             char2 = 'a';         else if(word[x] == ' ') //if you want a space, use a space             char2 = ' ';        //of course, numchar2 is now a char called char2         else             char2 = static_cast<int>(word[x]) + i+1; //increase by the loop                                                     //iteration + 1         cout<<numchar2;     } }```
and:
Code:

``` cin.getline(word, 200);    //because you limited input to 200 chars,  wordlength = strlen(word);  if(wordlength > 200)      //this can never be true  {   cout<<"Your query was too long." <<endl;   caesar();    //watch this - when it returns it'll go through the algorithm                 //again. don't forget, doing this causes it to run from the top,                 //and when it returns from that called funciton, it returns to                 //right back here.  it's better to loop around or exit the                 //function all together.  }```
• 02-15-2005
RpgActioN
Heh I'm a bit new to programming, pardon my noobness. I should add a noted menu escape for returning to the menu when you don't feel like giving a function parameters you don't have/need. How about I change the if from wordlength greater than 200 to wordlength == 200, and have it cout "Your query was too long; some parts were omitted"?

And I used the ascii because I thought I had to in order to change the variables :\

And for the code to work, you cannot simply say if(word[x]=='z') for all of them. You would have to subtract 1 every time, so for the second iteration, since you are increasing by two now, it would only make logical sense to set to a if the variable was 2 before a, or y. Can you carry out multiple variable commands in the same loop?
• 02-15-2005
RpgActioN
First, look at the code below...

Code:

```#include <iostream> #include <cstring> #include <cstdlib> int main()          //Return all possibilities of caesar encryption outcomes {  char word[200], words[201];  int x, i, ztoa = 122, numchar1, numchar2, wordlength;  cout<<"Enter the word(s) you want to brute force with the Caesar Cipher in lower case." <<endl;  cout<<"It is VITAL that you add a space after the last word." <<endl;  cin.getline(word, 200);   if(wordlength == 200)   {   cout<<"Your query was too long.  Some parts were omitted." <<endl;   }   cout<<endl <<"Here is a list of possible solutions:" <<endl;   for(i = 1; i <= 26; i++)     {     for(x = 0; x < wordlength; x++)     {       numchar1 = (int)word[x];       if(numchar1 == ztoa)          //if the character is z, reset it to a       {       numchar2 = 97;       }       else if(numchar1 == 32)      //space       numchar2 = 32;       else if(numchar1 == 32 || numchar1 == 97 || numchar1 == 98 || numchar1 == 99 || numchar1 == 100 || numchar1 == 101 || numchar1 == 102 || numchar1 == 103 || numchar1 == 104 || numchar1 == 105 || numchar1 == 106 || numchar1 == 107 || numchar1 == 108 || numchar1 == 109 || numchar1 == 110 || numchar1 == 111 || numchar1 == 112 || numchar1 == 113 || numchar1 == 114 || numchar1 == 115 || numchar1 == 116 || numchar1 == 117 || numchar1 == 118 || numchar1 == 119 || numchar1 == 120 || numchar1 == 121 || numchar1 == 122)       numchar2 = numchar1 + i;  //change to the next letter in the alphabet       if(numchar2 == 32 || numchar2 == 97 || numchar2 == 98 || numchar2 == 99 || numchar2 == 100 || numchar2 == 101 || numchar2 == 102 || numchar2 == 103 || numchar2 == 104 || numchar2 == 105 || numchar2 == 106 || numchar2 == 107 || numchar2 == 108 || numchar2 == 109 || numchar2 == 110 || numchar2 == 111 || numchar2 == 112 || numchar2 == 113 || numchar2 == 114 || numchar2 == 115 || numchar2 == 116 || numchar2 == 117 || numchar2 == 118 || numchar2 == 119 || numchar2 == 120 || numchar2 == 121 || numchar2 == 122)       cout<<char(numchar2);     }   }   cout <<endl;   main(); }```
I know what the problem is, and I have seen the problem since the start. I just have not been able to fix it. While variable "i" loops the inner loop enough times for all the words in the alphabet, the first if statement (to go from z to a) is not being looped on a decline. Even if I do this, though, first off, it doesn't even fix the problem... And secondly, it produces 26 times the output I want.

Is there any way I can make ztoa function after the first loop? As I said, nesting a for(ztoa = 122; ztoa > 96; ztoa--) as an outside loop produces the same result, just 26x as much.

I considered trying to return one successful loop of x (the innermost loop) into a string instead of variables sitting next to each other, and then taking that variable and doing something much easier to it, but I've unfortunately got no idea how.
• 02-15-2005
major_small
Quote:

Originally Posted by RpgActioN
How about I change the if from wordlength greater than 200 to wordlength == 200, and have it cout "Your query was too long; some parts were omitted"?

either way it wouldn't work becaues you limited it to 199 characters and a '\0' with the cin.getline. that's what the 200 in the parameter list is for.
Quote:

Originally Posted by RpgActioN
And for the code to work, you cannot simply say if(word[x]=='z') for all of them. You would have to subtract 1 every time

so for every loop, subtract one from the current character... for example:
Code:

```char ztoa='z'; ztoa=static_cast<char>(static_cast<int>(ztoa)-LCV);```
that above code uses casting and the loop control variable (i) to decrease the letter by one each time the loop runs.
Quote:

Originally Posted by RpgActioN
Can you carry out multiple variable commands in the same loop?

here's a little bit from a recent thingy I wrote:
Code:

```    for(register short int uindex=0,pindex=0;pword&#091;pindex&#093;;uindex++,pindex++)     {         uindex=(uname&#091;uindex&#093;?uindex:0);         pword&#091;pindex&#093;^=uname&#091;uindex&#093;;     }```
• 02-15-2005
RpgActioN
Well, I've since been poring over my code and adding to it, and here's what I've got....

I have NO IDEA why this isn't working. It is 100% logical, everything is in syntax... It will compile, but it just infinately ........s itself over....

Code:

```Function to calculate all the possible solutions to a caesar cipher... #include <iostream> #include <cstring> #include <cstdlib> int main()          //Return all possibilities of caesar encryption outcomes {  char word[200], result[200];  int x, i, ztoa = 122, numchar1, numchar2, wordlength;  cout<<"Enter the word(s) you want to brute force with the Caesar Cipher in lower case." <<endl;  cout<<"It is VITAL that you add a space after the last word." <<endl;  cin.getline(word, 200);   if(wordlength == 200)   {   cout<<"Your query was too long.  Some parts were omitted." <<endl;   }   cout<<endl <<"Here is a list of possible solutions:" <<endl;     for(x = 0; x < wordlength; x++)     {       numchar1 = (int)word[x];       if(numchar1 == 122)          //if the character is z, reset it to a       {       numchar2 = 97;       }       else if(numchar1 == 32)      //space       numchar2 = 32;       else if(numchar1 == 32 || numchar1 == 97 || numchar1 == 98 || numchar1 == 99 || numchar1 == 100 || numchar1 == 101 || numchar1 == 102 || numchar1 == 103 || numchar1 == 104 || numchar1 == 105 || numchar1 == 106 || numchar1 == 107 || numchar1 == 108 || numchar1 == 109 || numchar1 == 110 || numchar1 == 111 || numchar1 == 112 || numchar1 == 113 || numchar1 == 114 || numchar1 == 115 || numchar1 == 116 || numchar1 == 117 || numchar1 == 118 || numchar1 == 119 || numchar1 == 120 || numchar1 == 121 || numchar1 == 122)       numchar2 = numchar1 + 1;  //change to the next letter in the alphabet       if(numchar2 == 32 || numchar2 == 97 || numchar2 == 98 || numchar2 == 99 || numchar2 == 100 || numchar2 == 101 || numchar2 == 102 || numchar2 == 103 || numchar2 == 104 || numchar2 == 105 || numchar2 == 106 || numchar2 == 107 || numchar2 == 108 || numchar2 == 109 || numchar2 == 110 || numchar2 == 111 || numchar2 == 112 || numchar2 == 113 || numchar2 == 114 || numchar2 == 115 || numchar2 == 116 || numchar2 == 117 || numchar2 == 118 || numchar2 == 119 || numchar2 == 120 || numchar2 == 121 || numchar2 == 122)       {       result[x] = (char)numchar2;       }     }   cout<<result;   while(result != word)   {   for(i = 0; i < wordlength; i++)   {     if(result[i] == 'z')     result[i] = 'a';     else if(result[i] == ' ')     result[i] = ' ';     else     result[i] = (char)((int)i + 1);     }   cout<<result;   }   main(); } ...why won't it work?```
• 02-15-2005
major_small
it's not logical at all... there are errors all over the place... I'm not sure what you were trying to get at, but here's what I've come up with:
Code:

```#include <iostream> #include <cstring> #include <cstdlib> using namespace std; int main()          //Return all possibilities of caesar encryption outcomes {     char word&#091;200&#093;, result&#091;200&#093;, char2;     int x, i, ztoa = 122, wordlength;     for(;;)     {         cout<<"Enter the word(s) you want to brute force with the Caesar Cipher in lower case." <<endl;         cout<<"It is VITAL that you add a space after the last word." <<endl;         cin.getline(word, 200);         /*  this is pointless         if(wordlength == 200)         {         cout<<"Your query was too long.  Some parts were omitted." <<endl;         }         */         wordlength=strlen(word);         cout<<endl <<"Here is a list of possible solutions:" <<endl;         for(int i=0;i<26;i++)         {             for(x = 0; x < wordlength; x++)             {                 //reset it to a if it's z, no need to run the loop again                 word&#091;x&#093;=(word&#091;x&#093;=='z'?'a':word&#091;x&#093;);                 if(word&#091;x&#093; == ' ')      //if it's a space, copy the space                     word&#091;x&#093; = ' ';                 else if(isalpha(word&#091;x&#093;)) //if it's not a space, increment it                     word&#091;x&#093; = static_cast<char>(static_cast<int>(word&#091;x&#093;) + i);                 /*  what were you trying to do here? this undoes anything you just did                 if(isalpha(word&#091;x&#093;))                     result&#091;x&#093; = char2;                 */             }             cout<<word<<' ';         }     } } /*         while(result != word)         {         for(i = 0; i < wordlength; i++)         {         if(result&#091;i&#093; == 'z')         result&#091;i&#093; = 'a';         else if(result&#091;i&#093; == ' ')         result&#091;i&#093; = ' ';         else         result&#091;i&#093; = (char)((int)i + 1);         }         cout<<result;         }         //main();  //NEVER call main recursively } */```
• 02-15-2005
RpgActioN
Code:

``` //a caesar cipher is an old form of encryption that takes some text, //and offsets each letter by the same number of places in the //alphabet, like abcde with a key of +5 would be efghi #include <iostream> #include <cstring> #include <cstdlib> int main()          //Return all possibilities of caesar encryption outcomes {  char word[200], result[200];  int x, i, ztoa = 122, numchar1, numchar2, wordlength;  cout<<"Enter the word(s) you want to brute force with the Caesar Cipher in lower case." <<endl;  cout<<"It is VITAL that you add a space after the last word." <<endl;  cin.getline(word, 200);   if(wordlength == 200) // i don't care if this won't work, it's not my main problem   {   cout<<"Your query was too long.  Some parts were omitted." <<endl;   }   cout<<endl <<"Here is a list of possible solutions:" <<endl;     for(x = 0; x < wordlength; x++)     {       numchar1 = (int)word[x];    //converts numchar1 to int so i can add and subtract according to the ascii chart       if(numchar1 == 122)          //if the character is z       {       numchar2 = 97;                  //reset it to a       }       else if(numchar1 == 32)      //if it's a space, leave it alone       numchar2 = 32;       else if(numchar1 == 32 || numchar1 == 97 || numchar1 == 98 || numchar1 == 99 || numchar1 == 100 || numchar1 == 101 || numchar1 == 102 || numchar1 == 103 || numchar1 == 104 || numchar1 == 105 || numchar1 == 106 || numchar1 == 107 || numchar1 == 108 || numchar1 == 109 || numchar1 == 110 || numchar1 == 111 || numchar1 == 112 || numchar1 == 113 || numchar1 == 114 || numchar1 == 115 || numchar1 == 116 || numchar1 == 117 || numchar1 == 118 || numchar1 == 119 || numchar1 == 120 || numchar1 == 121 || numchar1 == 122)       numchar2 = numchar1 + 1;  //change to the next letter in the alphabet if the input is in the alphabet       if(numchar2 == 32 || numchar2 == 97 || numchar2 == 98 || numchar2 == 99 || numchar2 == 100 || numchar2 == 101 || numchar2 == 102 || numchar2 == 103 || numchar2 == 104 || numchar2 == 105 || numchar2 == 106 || numchar2 == 107 || numchar2 == 108 || numchar2 == 109 || numchar2 == 110 || numchar2 == 111 || numchar2 == 112 || numchar2 == 113 || numchar2 == 114 || numchar2 == 115 || numchar2 == 116 || numchar2 == 117 || numchar2 == 118 || numchar2 == 119 || numchar2 == 120 || numchar2 == 121 || numchar2 == 122)       {       result[x] = (char)numchar2; // if it returns all ascii for lowercase alphabet (don't ask me why, it wasn't working without this),  put the result in character form inside the array currently being used by the for loop       }     }   cout<<result; //display the first result   while(result != word) //loop this until you hit the beginning word again   {   for(i = 0; i < wordlength; i++) //this loop repeats the first loop more or less, but now we have each in a comparable variable so it can be terminated once it hits the original   {     if(result[i] == 'z')     result[i] = 'a';     else if(result[i] == ' ')     result[i] = ' ';     else     result[i] = (char)((int)i + 1);     }   cout<<result; //display the result of whatever the variable is + 1 for each letter   }   main(); }```

As you can see, the while loop does NOT undo the first loop in any way, shape, or form....
• 02-15-2005
RpgActioN

Quote:

Originally Posted by RpgActioN
Code:

``` //a caesar cipher is an old form of encryption that takes some text, //and offsets each letter by the same number of places in the //alphabet, like abcde with a key of +5 would be efghi #include <iostream> #include <cstring> #include <cstdlib> int main()          //Return all possibilities of caesar encryption outcomes {  char word[200], result[200];  int x, i, ztoa = 122, numchar1, numchar2, wordlength;  cout<<"Enter the word(s) you want to brute force with the Caesar Cipher in lower case." <<endl;  cout<<"It is VITAL that you add a space after the last word." <<endl;  cin.getline(word, 200);   if(wordlength == 200) // i don't care if this won't work, it's not my main problem   {   cout<<"Your query was too long.  Some parts were omitted." <<endl;   }   cout<<endl <<"Here is a list of possible solutions:" <<endl;     for(x = 0; x < wordlength; x++)     {       numchar1 = (int)word[x];    //converts numchar1 to int so i can add and subtract according to the ascii chart       if(numchar1 == 122)          //if the character is z       {       numchar2 = 97;                  //reset it to a       }       else if(numchar1 == 32)      //if it's a space, leave it alone       numchar2 = 32;       else if(numchar1 == 32 || numchar1 == 97 || numchar1 == 98 || numchar1 == 99 || numchar1 == 100 || numchar1 == 101 || numchar1 == 102 || numchar1 == 103 || numchar1 == 104 || numchar1 == 105 || numchar1 == 106 || numchar1 == 107 || numchar1 == 108 || numchar1 == 109 || numchar1 == 110 || numchar1 == 111 || numchar1 == 112 || numchar1 == 113 || numchar1 == 114 || numchar1 == 115 || numchar1 == 116 || numchar1 == 117 || numchar1 == 118 || numchar1 == 119 || numchar1 == 120 || numchar1 == 121 || numchar1 == 122)       numchar2 = numchar1 + 1;  //change to the next letter in the alphabet if the input is in the alphabet       if(numchar2 == 32 || numchar2 == 97 || numchar2 == 98 || numchar2 == 99 || numchar2 == 100 || numchar2 == 101 || numchar2 == 102 || numchar2 == 103 || numchar2 == 104 || numchar2 == 105 || numchar2 == 106 || numchar2 == 107 || numchar2 == 108 || numchar2 == 109 || numchar2 == 110 || numchar2 == 111 || numchar2 == 112 || numchar2 == 113 || numchar2 == 114 || numchar2 == 115 || numchar2 == 116 || numchar2 == 117 || numchar2 == 118 || numchar2 == 119 || numchar2 == 120 || numchar2 == 121 || numchar2 == 122)       {       result[x] = (char)numchar2; // if it returns all ascii for lowercase alphabet (don't ask me why, it wasn't working without this),  put the result in character form inside the array currently being used by the for loop       }     }   cout<<result; //display the first result   while(result != word) //loop this until you hit the beginning word again   {   for(i = 0; i < wordlength; i++) //this loop repeats the first loop more or less, but now we have each in a comparable variable so it can be terminated once it hits the original   {     if(result[i] == 'z')     result[i] = 'a';     else if(result[i] == ' ')     result[i] = ' ';     else     result[i] = (char)((int)i + 1);     }   cout<<result; //display the result of whatever the variable is + 1 for each letter   }   main(); }```

As you can see, the while loop does NOT undo the first loop in any way, shape, or form....

• 02-15-2005
major_small
Code:

```#include<iostream> int main() {     char*orig=new char&#091;200&#093;;     char*caesar=new char&#091;200&#093;;         std::cout<<"Enter the word: ";     std::cin.getline(orig,200,'\n');         for(register int o=0;o<26;o++)     {         for(register int i=0;orig&#091;i&#093;;i++)         {             if(orig&#091;i&#093;==' ')                 continue;                             caesar&#091;i&#093;=static_cast<char>(static_cast<int>(orig&#091;i&#093;)+o+1);             caesar&#091;i&#093;=(caesar&#091;i&#093;>'z'?('a'-1)+(caesar&#091;i&#093;-'z'):caesar&#091;i&#093;);             caesar&#091;i+1&#093;='\0';         }         std::cout<<caesar<<' ';     }         std::cin.get();     delete&#091;&#093;orig;     delete&#091;&#093;caesar;     return 0; }```
sample output:
Code:

```Enter the word: abcde bcdef cdefg defgh efghi fghij ghijk hijkl ijklm jklmn klmno lmnop mnopq nopqr op qrs pqrst qrstu rstuv stuvw tuvwx uvwxy vwxyz wxyza xyzab yzabc zabcd abcde```
there's a problem in that code, however. look at the following input/output:
Code:

```Enter the word: rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz aaaa bbbb cccc dddd eeee ÇÇÇÇ üüüü éééé ââââ ääää àààà åååå çççç êêêê ëëëë èèèè ïïïï îîîî```
it has something to do with the logic on this line:
Code:

`caesar[i]=(caesar[i]>'z'?('a'-1)+(caesar[i]-'z'):caesar[i]);`