C:未收到我期望的输出

问题描述 投票:0回答:2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int shift=2;

int getUserChoice()
{
    printf("--------------------------\n");
    printf("| 1: Change Shift        |\n");
    printf("| 2: Decrypt a message   |\n");
    printf("| 3: Encrypt a message   |\n");
    printf("| 4: Quit                |\n");
    printf("--------------------------\n");

    printf("What would you like to do?  ");

    int i=0;
    scanf("%d", &i);
    return i;
}

int getShift()
{
    int shiftChange;
    shiftChange=shift;
    return shiftChange;
}

void encrypt(char buf[], int shift)
{
    char string[100];
    int stringChanged[100];
    int i;
    int length;

    printf("Input: ");
    fgets(string, 100, stdin);
    length = strlen(string);

    do
    {
        i++;
        stringChanged[i] = string[i] - getShift();
        printf("%c\n", stringChanged[i]);
    } while (i != length);

}

void decrypt(char buf[], int shift)
{
        char string[100];
    int stringChanged[100];
    int i;
    int length;

    printf("Input: ");
    fgets(string, 100, stdin);
    length = strlen(string);

    do
    {
        i++;
        stringChanged[i] = string[i] - getShift();
        printf("%c\n", stringChanged[i]);
    } while (i != length);
}

int main()
{
    int a=0;

    do
    {
        a = getUserChoice();

        if (a = 9)
        {
            printf("Enter a new shift value");
            scanf("%d", &shift);
        }

        if (a=2)
        {
            decrypt;
        }

        if (a=3)
        {
            encrypt;
        }

    } while (a != 4);

}

代码旨在“加密邮件”和“解密”。它所做的只是将ascii值移动可变位移。我被困住了,程序在主函数中不断循环,而对getUserChoice()函数中的scanf没有响应。它一直在执行第一个if语句。我不太确定如何解决此问题,我对使用c编码是相当陌生的。

c caesar-cipher
2个回答
-1
投票

我希望这是您想要的结果。我确实尝试将您的代码更改为加密和解密,并且需要修复某些功能,例如调用函数,运算符,这些函数的返回等。


    /******************************************************************************

                    Program to encrypt/decrypt a message

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

#define MAX 100

int shift=2;

void getUserChoice()
{
    printf("--------------------------\n");
    printf("| 1: Change Shift        |\n");
    printf("| 2: Decrypt a message   |\n");
    printf("| 3: Encrypt a message   |\n");
    printf("| 4: Print the message   |\n");
    printf("| 5: Quit                |\n");
    printf("--------------------------\n");

    printf("What would you like to do?  :");


}

int getShift()
{
    int shiftChange;
    shiftChange = shift;
    return shiftChange;
}

char encrypt(char buf[], int shift )
{
    char string[MAX];
    char stringChanged[MAX];
    int i=0;
    int length;


    string[MAX] = buf[MAX];
    length = strlen(string);

    do
    {
        i++;
        stringChanged[i] = string[i] + getShift( );

    } while (i >= length);
     printf("\n");

    printf("Encrypted\n");
    return stringChanged[MAX];
}

char decrypt(char buf[], int shift)
{
    char string[MAX];
    char stringChanged[MAX];
    int i=0;
    int length;

    string[MAX] = buf[MAX];

    length = strlen(string);

    do
    {
        i++;
        stringChanged[i] = string[i] - getShift();

    } while (i >= length);
    printf("\n");
    printf("Decrypted\n");
    return stringChanged[MAX];
}

int main()
{
    int a;
    char mess_in[MAX];
    char mess_out[MAX];

    do
    {

        getUserChoice();
        scanf("%d", &a);
        //set buffer to use fgets
        setbuf(stdin, NULL);
        //changed because getUserChoice();
        // ==(op) instead = operator to comparison
        //i changed the if statment for switch
        switch(a){

            case 1:
                printf("Enter a new shift value :");
                scanf("%d", &shift);
                break;

            case 2:
                //functions would be pass parameters
                mess_in[MAX]=decrypt(mess_out,shift);
                printf("\nMessage(decrypted): %s\n",mess_in);
                break;

            case 3:
                printf("\nInput message(to encript): ");
                fgets(mess_in,MAX,stdin);
                mess_out[MAX]=encrypt(mess_in,shift);
                break;

            case 4:
                printf("\nMessage(encrypted): %s",mess_out);
                printf("\n\n");
                break;
            }

    } while (a != 5);

}


希望这对您有所帮助。


-2
投票

我认为您在主函数中调用加密和解密函数的方式错误。

© www.soinside.com 2019 - 2024. All rights reserved.