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

标签 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);
}

我正在尝试显示维吉尼亚广场,但创建它的 for 循环不会停止循环经过计数器。您可以在选项 1 下找到循环。此外,如果有人知道如何使解密函数起作用。

最佳答案

循环不会结束,因为 choice 不等于 4。您需要将用户输入部分放在 while 循环内,以便在每次迭代后再次询问用户选择。

int choice; 更改为 int choice = 0;(4 以外的值)并移动

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);

进入你的循环。

关于c - 为什么这个数组在c中不会停止循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46536915/

相关文章:

c - 在多线程程序中将全局数组锁定在需要重入的函数中?

c - 使用 scanf 读取行或中断特殊字符

c - 多线程,每个线程都有唯一的变量

c++ - 取决于字符串长度的段错误?

c - 不干净地从 C 子进程退出而没有 valgrind 提示?

c - C 左移运算中的模行为

c - 如何使用 DispCallFunc 在纯 C 中调用 IStream::SetSize(0)

javascript - 阵列帮助。如何将数组自身相乘,其中一个没有第一个元素,另一个没有最后一个元素

c++ - 如何将文本更改为语音以及如何将字符插入字符数组

arrays - 替换 2 个数组的元素?