我正处于解决这个问题的边缘。只是看......任何人都可以告诉我应该改变什么?

时间:2023-01-01 21:27:43
#include <stdio.h>
int main ()
{
    char name[20];

    printf("Type your name please: ");
    fgets(name,20,stdin);

    switch(name[20])
    {
    case 'name1':
        printf("\nYou are good\n");
        break;
    case 'name2':
        printf("\nYou are not so good\n");
        break;
    case 'name3':
        printf("\nYou are bad\n");
        break;
    case 'name4':
        printf("\nYou are very ba\n");
        break;
    default:
        printf("\nYou entered an invalid letter\n");
        break;
    }
    return 0;
}

2 个解决方案

#1


3  

You can't use switch to compare strings. It only works on integer types. Also, string constants are denoted with double quotes ("), not single quotes ('). You need to use strcmp to compare strings:

您不能使用开关来比较字符串。它仅适用于整数类型。此外,字符串常量用双引号(“)表示,而不是单引号(')。您需要使用strcmp来比较字符串:

if (!strcmp(name, "name1")) {
    printf("\nYou are good\n");
} else if (!strcmp(name, "name2")) {
    printf("\nYou are not so good\n");
} else if (!strcmp(name, "name3")) {
    printf("\nYou are bad\n");
} else if (!strcmp(name, "name4")) {
    printf("\nYou are very ba\n");
} else {
    printf("\nYou entered an invalid letter\n");
}

#2


1  

switch(expression)

开关(表达式)

The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

switch语句中使用的表达式必须具有整数或枚举类型,或者是类类型,其中类具有单个转换函数为整数或枚举类型。

You can't use switch to compare two strings as dbush said.It is better to compare the two strings and print the desired result.

您不能使用开关比较两个字符串,因为dbush说。比较两个字符串并打印所需的结果更好。

You should also remove the trailing newline character as otherwise you won`t get the desired result:

您还应该删除尾随的换行符,否则您将无法获得所需的结果:

fgets(name,20,stdin);
len = strlen(name);
name[len - 1] = '\0';

#1


3  

You can't use switch to compare strings. It only works on integer types. Also, string constants are denoted with double quotes ("), not single quotes ('). You need to use strcmp to compare strings:

您不能使用开关来比较字符串。它仅适用于整数类型。此外,字符串常量用双引号(“)表示,而不是单引号(')。您需要使用strcmp来比较字符串:

if (!strcmp(name, "name1")) {
    printf("\nYou are good\n");
} else if (!strcmp(name, "name2")) {
    printf("\nYou are not so good\n");
} else if (!strcmp(name, "name3")) {
    printf("\nYou are bad\n");
} else if (!strcmp(name, "name4")) {
    printf("\nYou are very ba\n");
} else {
    printf("\nYou entered an invalid letter\n");
}

#2


1  

switch(expression)

开关(表达式)

The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

switch语句中使用的表达式必须具有整数或枚举类型,或者是类类型,其中类具有单个转换函数为整数或枚举类型。

You can't use switch to compare two strings as dbush said.It is better to compare the two strings and print the desired result.

您不能使用开关比较两个字符串,因为dbush说。比较两个字符串并打印所需的结果更好。

You should also remove the trailing newline character as otherwise you won`t get the desired result:

您还应该删除尾随的换行符,否则您将无法获得所需的结果:

fgets(name,20,stdin);
len = strlen(name);
name[len - 1] = '\0';