C - 系统( "cls" ) 是否更改了我的变量?



我是C语言的新手,编写这个程序只是为了好玩。但后来我遇到了一个问题。每次system("cls")出现时,encryptedWord似乎都会擦除它的值。事情应该是那样的吗?如果是这样,你有什么替代的建议吗?

下面是我的代码:

const char * dencrypt(int numeric, const char * encryptedWord);
int getKey();
void displayKey(int number, char *keyUp, char *keyLow, int num, char *keys);
int main(void) {
    int ans = 'y';
    int choice;
    const char * encryptedWord = " ";
    int count = 1;

    while (ans == 'y' || ans == 'Y') {
        system("cls");
        printf("This program will encrypt and decrypt message accordingn");
        printf("to your choice.nn");
        puts ("Pick your choice..");
        puts ("1. Encrypt");
        puts ("2. Decrypt");
        printf("n");
        printf("Enter choice: ");
        scanf("%d", &choice);
        getchar();
        while (choice < 1 || choice > 2) {
            puts("Sorry kid, only 1 or 2.");
            printf("Enter choice: ");
            scanf("%d", &choice);
            getchar();
        }
        switch(choice) {
        case 1: 
            system("cls");
            encryptedWord = dencrypt(choice, encryptedWord);
            break;
        case 2:
            if (count > 1) {
                system("cls");
                printf("nDo you want to use your last encrypted word?n");
                printf("Note that if you pick NO, your encrypted wordnwill be erased from the memory..");
                printf("n1. YES");
                printf("n2. NO");
                printf("nInput choice: ");
                scanf("%d", &choice);
                getchar();
                system ("cls");
                while(choice < 1 || choice > 2) {
                    printf("nPfft.. pick only 1 or 2.");
                    printf("nInput choice: ");
                    scanf("%d", &choice);
                    getchar();
                } 
                switch (choice) {
                    case 1:
                        printf("nLast Encrypted Word: %sn", encryptedWord);
                        break;
                    case 2:
                        break;
                }
            } else {
                system("cls");
            }
            dencrypt(choice, encryptedWord);
            break;
        }
        printf("nnDo you want to continue?n");
        printf("Note that in doing so the console will be cleared");
        printf("nPress Y to continue or any other key to end this program.n");
        ans = getchar();
        getchar();
        count++;
    }
}

const char * dencrypt(int numeric, const char * rWord) {
    int keyStr, num,i, s, total;
    int c;
    bool found;
    char word[SIZE];
    char fWord[SIZE];
    char keyUp[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    char keyLow[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    char keys[25];
    switch (numeric) {
    case 1:
        puts("Input key for encryption");
        break;
    case 2:
        puts("Input key for decryption");
        break;
    }
    num = getKey() - 'A';
    displayKey(numeric , keyUp, keyLow, num, keys);
    printf("nn");
    switch(numeric) {
    case 1:
        puts("Input a word to be encrypted");
        break;
    case 2: 
        puts("Input a word to be decrypted");
        break;
    }
    i = 0;
    total = 0;
    while(i < SIZE - 1 && (c = getchar()) != 'n') {
        word[i++] = c;
        total++;
    }
    word[i] = '';
    printf("n");
    puts("Original Word: ");
    printf("%snn", word);
    switch(numeric) {
    case 1:
        printf("Encrypted word: n");
        for (i = 0; i <= total - 1; i++) {
            found = false;
            s = 0;
            if (islower(word[i])) {
                while (found != true) {
                    if (word[i] == keyLow[s]) {
                        found = true;
                    } else {
                        s++;
                    }
                }
                s += num;
                if (s > 25) {
                    s -= 26;
                }
                printf("%c", keyLow[s]);
                fWord[i] = keyLow[s];
            } else if (isupper(word[i])) {
                while (found != true) {
                    if (word[i] == keyUp[s]) {
                        found = true;
                    } else {
                        s++;
                    }
                }
                s += num;
                if (s > 25) {
                    s -= 26;
                }
                printf("%c", keyUp[s]);
                fWord[i] = keyUp[s];
            } else if(isspace(word[i])) {
                printf(" ");
                fWord[i] = ' ';
            } else {
                printf("%c", word[i]);
                fWord[i] = word[i];
            }
        }
        printf("nnn");
        rWord = fWord;
        break;
    case 2:
        printf("Decrypted word: n");
        for (i = 0; i <= total - 1; i++) {
            found = false;
            s = 0;
            if (islower(word[i])) {
                while (found != true) {
                    if (word[i] == keyLow[s]) {
                        found = true;
                    } else {
                        s++;
                    }
                }
                s -= num;
                if (s < 0) {
                    s += 26;
                }
                printf("%c", keyLow[s]);
            } else if (isupper(word[i])) {
                while (found != true) {
                    if (word[i] == keyUp[s]) {
                        found = true;
                    } else {
                        s++;
                    }
                }
                s -= num;
                if (s < 0 ) {
                    s += 26;
                }
                printf("%c", keyUp[s]);
            } else if(isspace(word[i])) {
                printf(" ");
            } else {
                printf("%c", word[i]);
            }
        }
        printf("nnn");
        break;
    }
    return rWord;
}

你没有发布dencrypt()的代码,但我的精神力量告诉我这就是错误所在:这个函数返回本地缓冲区的地址(一个大的禁忌),它被后续的函数调用覆盖。

您需要在返回缓冲区之前动态分配缓冲区,或者在main()中分配它并将其作为参数传递给dencrypt()

最新更新