简体   繁体   English

编码/解码程序的逻辑错误 (C)

[英]Logic error with an encode/decode program (C)

I am a beginner in a C programming class, and our assignment for the third chapter of our textbook was to create an encode/decode program that takes in four digits from user input and rearranges them depending on whether or not the user wants them to be either encoded or decoded.我是 C 编程课程的初学者,我们教科书第三章的任务是创建一个编码/解码程序,该程序从用户输入中获取四位数字,并根据用户是否希望将它们重新排列编码或解码。 I am also having trouble with getting the program to do both decoding and encoding in one file .我也无法让程序在一个文件中同时进行解码和编码。 However, when I fire up the program, whenever I select encode and type in my four digits, the program not only gives me a massive number that should only be four digits as the encoded result.然而,当我启动程序时,每当我选择编码并输入我的四位数字时,程序不仅会给我一个应该只有四位数字作为编码结果的大量数字。 It also gives me the "press any key to continue" prompt, signifying the end of the program, instead of starting over completely and going back to where it asks the user to either decode or encode.它还给了我“按任意键继续”提示,表示程序结束,而不是完全重新开始并返回到它要求用户解码或编码的地方。

The output is also attached.输出也附上。


I've tried looking around and changing some of the variables from int to double, as well as checking my math... I'm not 100% sure why the encryption and decryption aren't limited to anything besides four digits.我试过环顾四周并将一些变量从 int 更改为 double,并检查我的数学......我不是 100% 确定为什么加密和解密不限于四位数以外的任何内容。

As for getting the program to start over, I've tried using a do-while loop, but it still didn't seem to work, so I deleted it.. I also tried using another if statement and then using break, but it was marked as an error.至于让程序重新开始,我试过使用do-while循环,但它似乎仍然不起作用,所以我删除了它..我也尝试使用另一个if语句然后使用break,但它被标记为错误。


The assignment says that, "each digit should be encrypted by adding 7 and taking the remainder after division by 10; after encrypting each digit, swap the first and third digits, and then swap the second and fourth digits. Decryption should reverse the process. Program must do encode and decode in one file.作业说,“每个数字都应该通过加7并除以10后的余数进行加密;每个数字加密后,交换第一和第三个数字,然后交换第二和第四个数字。解密应该反转这个过程。程序必须在一个文件中进行编码和解码。

Here is the example that my instructor posted for this, this is what the output should look like.这是我的讲师为此发布的示例,这就是输出的样子。

Enter a four digit number: 1234
Encoded Digits: 0189
Continue (1) Exit (0): 1
Encode (1) Decode (2): 2
Enter a four digit number: 0189
Decoded Digits: 1234
Continue (1) Exit (0): 0

Here is my code:这是我的代码:

///Compiler used: Microsoft Visual Studio
///Language: C
#include <string>
#pragma warning(disable: 4996) 
#include <math.h>
#include<stdio.h>
#include<stdlib.h>

int main()
{

    int Encodechoice = 1;
    int inputdigit1 = 0;
    int Decodechoice = 2;
    int UserChoice = 0;
    int ContinueChoice = 0;
    int UserDigit1 = 0;
    int UserDigit2 = 0;
    int UserDigit3 = 0;
    int UserDigit4 = 0;

    {
        printf("(1) Encode, (2) Decode\n");
        printf("Make your selection.\n");//Asks user to make selection
        scanf_s("%d", &UserChoice);

        if (UserChoice == 1)//begin encryption

        {
            UserDigit1 = 0;
            UserDigit2 = 0;
            UserDigit3 = 0;
            UserDigit4 = 0;
            printf("Encode: Enter FOUR integers.\n");
            scanf_s("%d", &UserDigit1, &UserDigit2, &UserDigit3, &UserDigit4);
            int EncodedIntegers1 = (UserDigit1 + 7) % 10;
            int EncodedIntegers2 = (UserDigit2 + 7) % 10;
            int EncodedIntegers3 = (UserDigit3 + 7) % 10;
            int EncodedIntegers4 = (UserDigit4 + 7) % 10;
            printf("Encoded result:\n");
            printf("%d", "%d", "%d", "%d\n", EncodedIntegers3, &EncodedIntegers4, &EncodedIntegers1, &EncodedIntegers2); /// swap order of integers
        }///end if

        if (UserChoice == 2)///begin decryption
        {
            UserDigit1 = 0;
            UserDigit2 = 0;
            UserDigit3 = 0;
            UserDigit4 = 0;
            printf("Decode: Enter FOUR integers.\n");
            scanf_s("%d", &UserDigit1, &UserDigit2, &UserDigit3, &UserDigit4);
            int DecodedIntegers1 = (UserDigit1 - 7) * 10;
            int DecodedIntegers2 = (UserDigit2 - 7) * 10;
            int DecodedIntegers3 = (UserDigit3 - 7) * 10;
            int DecodedIntegers4 = (UserDigit4 - 7) * 10;
            printf("Decoded result:\n");
            printf("%d", "%d", "%d", "%d\n", DecodedIntegers1, &DecodedIntegers2, &DecodedIntegers3, &DecodedIntegers4); /// keep order the same to decrypt
        }///end if
        system("pause");
        return 0;
    }
}

Output:输出:

Make your selection.
1
Encode: Enter FOUR integers.
1234
Encoded result:
11893608 Continue? (1) Yes, (0) No
Make your selection.
1
Press any key to continue . . .```


You scanf is wrong.你扫描错了。 I would rather do this我宁愿这样做

char UserInputNumber[5];
printf("Encode: Enter FOUR integers.\n");
scanf("%s", UserInputNumber);
// optional error checking the input: length is 4? All char is a number?
UserDigit1 = (UserInputNumber[0] - '0');
UserDigit2 = (UserInputNumber[1] - '0');
UserDigit3 = (UserInputNumber[2] - '0');
UserDigit4 = (UserInputNumber[3] - '0');

Also your printf should look like this:您的 printf 也应如下所示:

printf("%d%d%d%d\n", EncodedIntegers3, EncodedIntegers4, EncodedIntegers1, EncodedIntegers2);

With the & you tried to print their memory address not their value, but also your format string was wrong too.使用&你试图打印它们的内存地址而不是它们的值,而且你的格式字符串也是错误的。

The easiest way is probably work directly on a string consisting of the four digits:最简单的方法可能是直接处理由四位数字组成的字符串:

char d[5], e[5] = { 0 };
scanf("%4s", d)

This is for encoding:这是用于编码:

for (int i = 0; i < 4; i++) e[(i+2)%4]=(d[i]-41)%10+'0';

For decoding, you have to change the magic 41 to 45 .对于解码,您必须将魔法41更改为45 The result is in e .结果在e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM