不接受用户输入的其他选项

Not accepting other option entered by user

我要用C做凯撒密码,加解密取决于密钥和密码(最多4个字符)加上移动方向,这三者都是用户给定的。 在我的代码中,它只需要向左,当我向右输入时,它会再次重新询问方向。不知道怎么回事

这是我的代码:

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

char pass[4];
char shiftdirection[6];
char left[6] = "left";
char right[6] = "right";
char ch;
int i, key;

void readline (){ //cette fonction prend de l'utilisateur le password et la clé
    do{
    printf("Please enter the pass, max 4 characters: \n");
    gets(pass);
    }while((strlen(pass)>4) || (strlen(pass)<4)); //get the password from th
    printf("Please enter the key: \n");
    scanf("%d",&key);
}

//cette fonction utilisée pour chiffrer
void encrypt(){
    printf("Enter the direction: \n");
    scanf("%s", &shiftdirection);
    while ((strcmp(shiftdirection,left)==1) || (strcmp(shiftdirection,right)==1)) { // demander de l'utilisateur la direction de décalage

        printf("Enter the direction: \n");
        scanf("%s", &shiftdirection);
    }
    if(strcmp(shiftdirection,left)==0){ // si l'utilisateur a écrit left, le programme commence le chiffrage avec un décalage à gauche
    for( i = 0; pass[i] != '[=10=]'; i++){
        ch = pass[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = (char) (ch + key);
            if(ch > 'z'){
                ch = (char) (ch - 'z' + 'a' - 1);
            }
            pass[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            ch = (char) (ch + key);
            if(ch > 'Z'){
                ch = (char) (ch - 'Z' + 'A' - 1);
                }
            pass[i] = ch;
            }
        }
    }
   else if(strcmp(shiftdirection,right)==0){ // si l'utilisateur a écrit left, le programme commence le chiffrage avec un décalage à gauche
        for( i = strlen(pass) - 1; i >= 0; --i){
            ch = pass[i];
            if(ch >= 'a' && ch <= 'z'){
                ch = (char) (ch + key);
                if(ch > 'z'){
                    ch = (char) (ch - 'z' + 'a' - 1);
                }
                pass[i] = ch;
            }
            else if(ch >= 'A' && ch <= 'Z'){
                ch = (char) (ch + key);
                if(ch > 'Z'){
                    ch = (char) (ch - 'Z' + 'A' - 1);
                }
                pass[i] = ch;
            }
        }
    }
    printf("Encrypted Password is %s", pass); // le password chiffré
}

//cette fonction utilisée pour déchiffrer
void decrypt(){
    for(i = 0 ; pass[i] != '[=10=]' ; ++i){
        ch = pass[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = (char) (ch - key);
            if(ch < 'a') {
                ch = (char) (ch + 'z' - 'a' + 1);
            }
            pass[i] = ch;
        } else if (ch >= 'A' && ch <= 'Z'){
            ch = (char) (ch - key);
            if(ch < 'A'){
                ch = (char) (ch + 'Z' - 'A' + 1);
            }
            pass[i] = ch;
        }
    }
    printf("\nDecrypted Password is %s", pass); //le password déchiffré
}
//main
int main() {
    readline();
    encrypt();
    decrypt();
    return 0;
}

  1. 正如评论中提到的那样,您必须更改 char pass[4] -> char pass[5] 以考虑按 return 的 [=12=]\n
  2. 不要使用gets。使用 fgets 而不是因为 gets 不安全并且不再是 C 的一部分
  3. 使用getchar()通过按下return
  4. 来捕获剩下的[=12=]
  5. scanf 更改为 scanf_s 以获得更安全的
  6. 您的 while ((strcmp(shiftdirection,left)==1) || (strcmp(shiftdirection,right)==1)) 检查是否有一个条件不成立,然后进入。您需要检查这两个条件是否都不成立,因此您需要 && 而不是 ||

我稍微研究了一下您的代码,得出了以下结论。

代码:

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

char pass[5];
char shiftdirection[6];
char left[6] = "left";
char right[6] = "right";
char ch;
int i, key;

void readline (){ //cette fonction prend de l'utilisateur le password et la clé
    do{
    printf("Please enter the pass, max 4 characters: \n");
    fgets(pass, sizeof pass, stdin);
    }while((strlen(pass)>4) || (strlen(pass)<4)); //get the password from th
    printf("Please enter the key: \n");
    scanf_s("%d",&key);
    getchar();
}

//cette fonction utilisée pour chiffrer
void encrypt(){
    printf("Enter the direction: \n");
    fgets(shiftdirection, sizeof shiftdirection, stdin);
    shiftdirection[strcspn(shiftdirection, "\n\r")] = 0;
    while ((strcmp(shiftdirection,left)==1) && (strcmp(shiftdirection,right)==1)) { // demander de l'utilisateur la direction de décalage

        printf("Enter the direction: \n");
        fgets(shiftdirection, sizeof shiftdirection, stdin);
        shiftdirection[strcspn(shiftdirection, "\n\r")] = 0;
    }
    if(strcmp(shiftdirection,left)==0){ // si l'utilisateur a écrit left, le programme commence le chiffrage avec un décalage à gauche
    for( i = 0; pass[i] != '[=10=]'; i++){
        ch = pass[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = (char) (ch + key);
            if(ch > 'z'){
                ch = (char) (ch - 'z' + 'a' - 1);
            }
            pass[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            ch = (char) (ch + key);
            if(ch > 'Z'){
                ch = (char) (ch - 'Z' + 'A' - 1);
                }
            pass[i] = ch;
            }
        }
    }
   else if(strcmp(shiftdirection,right)==0){ // si l'utilisateur a écrit left, le programme commence le chiffrage avec un décalage à gauche
        for( i = strlen(pass) - 1; i >= 0; --i){
            ch =pass[i];
            if(ch >= 'a' && ch <= 'z'){
                ch = (char) (ch + key);
                if(ch > 'z'){
                    ch = (char) (ch - 'z' + 'a' - 1);
                }
                pass[i] = ch;
            }
            else if(ch >= 'A' && ch <= 'Z'){
                ch = (char) (ch + key);
                if(ch > 'Z'){
                    ch = (char) (ch - 'Z' + 'A' - 1);
                }
                pass[i] = ch;
            }
        }
    }
    printf("Encrypted Password is %s", pass); // le password chiffré
}

//cette fonction utilisée pour déchiffrer
void decrypt(){
    for(i = 0 ; pass[i] != '[=10=]' ; ++i){
        ch = pass[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = (char) (ch - key);
            if(ch < 'a') {
                ch = (char) (ch + 'z' - 'a' + 1);
            }
            pass[i] = ch;
        } else if (ch >= 'A' && ch <= 'Z'){
            ch = (char) (ch - key);
            if(ch < 'A'){
                ch = (char) (ch + 'Z' - 'A' + 1);
            }
            pass[i] = ch;
        }
    }
    printf("\nDecrypted Password is %s", pass); //le password déchiffré
}
//main
int main() {
    readline();
    encrypt();
    decrypt();
    system("pause");
    return 0;
}

注意 shiftdirection[strcspn(shiftdirection, "\n\r")] = 0; 检查字符串是否有剩余 \n 因此 getchar() 可能会被替换。

希望这就是您要找的

编辑: 你的 while((strlen(pass)>4) || (strlen(pass)<4)); 基本上是 while(strlen(pass)!=4); 这意味着 pass 应该总是 4 个字符长。是这样吗?或者用户也可以输入小于 4?

更新:

检查了

while ((strcmp(shiftdirection,left)==1) && strcmp(shiftdirection,right)==1)) 再次条件,因为它是错误的。

改成 while ((strcmp(shiftdirection,left)!=0) && (strcmp(shiftdirection,right)!=0))

strcmp(shiftdirection,left)strcmp(shiftdirection,right)相应地不是leftright时,该值是-1而不是1