0

My vigenere program, compiles and has most of the check50 test-thingy right. However, I cannot get the program to print out the cipher. Not sure what is legal here or not but I have gone over and made sure that I haven't placed brackets or the ; in the wrong places. It is similar to the caesar loop-- and that printed out. What am I doing wrong? All the other aspects of the program check out ok. I've also checked to make sure all the open closed brackets match.

for(int i = 0, n = strlen(p); i < n; i++ )//ptext loop
    {
        for(int j = 0, m = strlen(k); j % m; j++) //cipher loop        
        { 
            if (p[i] >= 65 && p[i] <= 90)
                {
                c = (p[i] + (k[j] -65)) % 26;
                printf("%c", c);
                }                   

            else if (p[i] >= 97 &&  p[i] <= 122)
                {
                a = (p[i] + (k[j]- 97)) % 26;
                printf("%c", a);
                }   
            else 
                {
                printf("%c", p[i]);
                j--;
                }
        }
1
  • Thanks, got the cipher set up now correctly. Jun 9 '16 at 23:33
0

The inner for loop will never execute. Look at the setup:

for(int j = 0, m = strlen(k); j % m; j++)

The loop will only execute as long as the test evaluates as true. When the test condition is false, the loop terminates and the code passes control to the first line after the loop. With that in mind, look at the test condition, j % m;. At the start of the loop, j=0. With modulo, 0 % x always evaluates to 0 and in a for loop test, 0 always evaluates as false, so the for loop will never execute.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .