为什么这个数组不会在c中停止循环

时间:2017-10-03 03:47:47

标签: c arrays

我正在尝试显示Vigenere Square,但创建它的for循环不会停止循环通过计数器。您可以在选择1下找到循环。此外,如果有人知道如何使解密功能起作用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char encrypt(char, char);
char decrypt(char, char);

int main()
{
    int choice;
    printf("What would you like to do?\n\n");
    printf("1. Show board\n");
    printf("2. Encrypt\n");
    printf("3. Decrypt\n");
    printf("4. Exit\n");

    scanf("%i", &choice);
    while(choice != 4){
        char key[9];
        char input[51];
        char decrypt[51];

        if (choice == 3 || choice == 2){
            printf("Give me a 9 character or less key in caps.\n");
            scanf("%s", &key);
            printf("Give me a 51 character or less string in caps.\n");
            scanf("%s", &input);
        }

        if (choice==1){
            int i, j;
            for(i=0;i<=25;i++){
                for(j=0;j<=25;j++){
                    printf("%c  ", encrypt((char) i, (char) j));
                }
                printf("\n");
            }

        } else if (choice == 2){
            int i;
            char text_crypt[51];

            for(i = 0; i < strlen(input); i++){
                text_crypt[i] = encrypt(input[i], key[i % strlen(key)]);
            }
            printf("cyphertext is: %s\n", text_crypt);
        } else if (choice == 3){
            int i;
            char text_crypt[51];

            for(i = 0; i < strlen(input); i++){
                text_crypt[i] = encrypt(input[i], key[i % strlen(key)]);
            }
            printf("cyphertext is: %s\n", text_crypt);

        }
    }
return 0;
}


char encrypt(char x, char y){
    return 65+((x+y)%26);
}

char decrypt(char x, char y){
    return 90-((x+y)%26);
}

我正在尝试显示Vigenere Square但是创建它的for循环不会停止循环通过计数器。您可以在选择1下找到循环。此外,如果有人知道如何使解密功能起作用。

1 个答案:

答案 0 :(得分:2)

循环没有结束,因为选择不等于4.你需要在while循环中有用户输入部分,所以在每次迭代后它再次请求用户选择。

$(LOCAL_PATH)/..更改为int choice;(4以外的其他内容)并移动

int choice = 0;

进入你的循环。

相关问题