系统(“cls”)改变了我的变量吗?

时间:2014-03-13 17:23:59

标签: c system

我是C的新手,我只是为了好玩这个程序。但后来我遇到了一个问题。 每次encryptedWord发生时,system("cls")似乎都会删除其值。它应该以这种方式发生吗?如果是的话,你有什么建议可以替代吗?

这是我的代码:

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 according\n");
        printf("to your choice.\n\n");
        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 word\nwill 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: %s\n", encryptedWord);
                        break;
                    case 2:
                        break;
                }

            } else {
                system("cls");
            }
            dencrypt(choice, encryptedWord);
            break;
        }
        printf("\n\nDo 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("\n\n");

    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] = '\0';

    printf("\n");
    puts("Original Word: ");
    printf("%s\n\n", 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("\n\n\n");
        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("\n\n\n");
        break;
    }
    return rWord;
}

1 个答案:

答案 0 :(得分:3)

你没有发布dencrypt()的代码,但是我的通灵能力告诉我错误在哪里:这个函数返回一个本地缓冲区的地址(一个大禁忌),由后续函数调用覆盖。

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