gpt4 book ai didi

c - 为什么程序不执行切换?

转载 作者:行者123 更新时间:2023-11-30 14:43:18 25 4
gpt4 key购买 nike

这是一个简单的密码。我不太明白为什么 switch 语句不会执行。 switch 语句之前的所有内容都有效。如果您能简要解释如何解决此问题,那就太好了。我最近开始用 C 编码,所以如果这是一个愚蠢的问题,我很抱歉!预先感谢您!

#include <stdio.h>
#include <string.h>

int main()
{

char letter;
char word[100];

int i;
int j;
int x;

int shift;
int stringLength;

printf("Enter d to DECRYPT.\n");
printf("Enter e to ENCRYPT.\n");

scanf("%c", & letter);

if(letter == 'd' || letter == 'D') // d = 0, e = 1
{
x = 0;
}
else if(letter == 'e' || letter == 'E')
{
x = 1;
}
else
{
printf("The letter you entered was neither d or e!\n");
return 0;
}

printf("\nEnter the number of shifts:\n");
scanf("%d", &shift);

printf("\nEnter the word or phrase you would like to Encrypt or Decrpyt\n");
scanf("%s", word);
//printf("\nThe word you entered was:\n%s\n", word);

stringLength = strlen(word);
//printf("\nThe size of the string is: %i\n", stringLength);

switch(x)
{
case 0: // decrypt

for(i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] - shift;

break;

/*
* while(i < stringLength)
* {
* word[i] = word[i] - shift;
* i++;
*
* printf("The Decrypted word is: %s\n", word);
*/
//break;

case 1: // encrypt

for(i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] + shift;

break;


/*
* while( i < stringLength)
* {
* word[i] = word[i] + shift;
* i++;
*
* printf("The Encrypted word is: %s\n", word);
*/
}

return 0;
}

最佳答案

您的switch正在执行,但每个case的正文中有太多注释掉的部分。

使用 fgets 而不是 scanf 作为短语,因为 scanf 只会获取第一个单词。

旁注:我会使用 #if 0#endif 而不是 /**/ 注释掉代码块。

请注意,虽然您可以混合使用 scanffgets,但我已重新编码为仅使用 fgetsstrtol

这是清理后的工作代码[请原谅无偿的清理方式]:

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

char word[100];

void
get(void)
{

fgets(word,sizeof(word),stdin);
char *cp = strchr(word,'\n');
if (cp != NULL)
*cp = 0;

}

int
main()
{

char letter;

int i;
int j;
int x;

int shift;
int stringLength;

printf("Enter d to DECRYPT.\n");
printf("Enter e to ENCRYPT.\n");

get();
letter = word[0];

if (letter == 'd' || letter == 'D') // d = 0, e = 1
{
x = 0;
}
else if (letter == 'e' || letter == 'E') {
x = 1;
}
else {
printf("The letter you entered was neither d or e!\n");
return 0;
}

printf("\nEnter the number of shifts:\n");
get();
shift = strtol(word,NULL,10);

printf("\nEnter the word or phrase you would like to Encrypt or Decrpyt\n");
#if 0
scanf("%s", word);
#else
get();
#endif
printf("\nThe word you entered was:\n%s\n", word);

stringLength = strlen(word);
printf("\nThe size of the string is: %i\n", stringLength);

printf("x=%d\n", x);
switch (x) {
case 0: // decrypt
printf("decrypt case\n");

for (i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] - shift;

#if 0
while (i < stringLength) {
word[i] = word[i] - shift;
i++;
}

#endif
printf("The Decrypted word is: %s\n", word);
break;

case 1: // encrypt
printf("encrypt case\n");
for (i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] + shift;

#if 0
while (i < stringLength) {
word[i] = word[i] + shift;
i++;
}
#endif

printf("The Encrypted word is: %s\n", word);
break;
}

return 0;
}

关于c - 为什么程序不执行切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53981962/

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