gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:19:28 25 4
gpt4 key购买 nike

我是 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 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;
}

最佳答案

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

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

关于c - 系统 ("cls") 是否更改了我的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com