C语言 如何检查变量是否等于多个变量

tkqqtvp1  于 2023-08-03  发布在  其他
关注(0)|答案(2)|浏览(199)

我目前正在学习“CS50计算机科学入门”课程,我对C编程还是个新手。我正在做一个简单的协议项目,涉及使用if语句。然而,作为一个初学者,我在编写代码时遇到了一些困难。
如果有任何有C编程经验的人可以伸出援助之手,我将非常感谢您的帮助。下面是我到目前为止尝试过的代码:

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

int main(void)
{
    string username = get_string("What would you like your username to be? ");
    string agreement = get_string("Do you agree to the following terms & conditions? ");
    if (!strcmp(agreement, "yes" || agreement, "y" || agreement, "Yes" || agreement, "Y"))
    {
        printf("You can now continue using the service, %s! \n", username);
    }
    else if (!strcmp(agreement, "no" || agreement, "n" || agreement, "Yes" || agreement, "Y"))
    {
        printf("You need to agree to the terms & conditions to use the service\n");
    }
    else
    {
        printf("You need to select a option\n");
    }
}

字符串
下面是当我尝试编译代码时抛出的错误:

Too many arguments for function call. Expected 2, Have 5


我试着在谷歌上搜索类似的问题,发现了这样的结果:How to check if variable equal to multiple values但是,我无法解决我的问题。以下是我尝试的代码(不起作用):

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

int main(void)
{
    string username = get_string("What would you like your username to be? ");
    string agreement = get_string("Do you agree to the following terms & conditions? ");
    if (str[agreement] == 'yes' || str[agreement] == 'y' || str[agreement] == 'Yes' || str[agreement] == 'Y')
    {
        printf("You can now continue using the service %s! \n", usename);
    }
    else if (str[agreement] == 'no' || str[agreement] == 'n' || str[agreement] == 'No' || str[agreement] == 'N')
    {
        printf("You need to agree to the terms & conditions to use the service\n");
    }
    else
    {
        printf("You need to select a option\n");
    }
}


但我收到了这个错误:

Use of undeclared identifier "str"


我很兴奋地学习和欣赏任何指导或提示,你可以提供帮助我在这个项目上取得进展。非常感谢您的时间和支持!

bzzcjhmw

bzzcjhmw1#

strcmp需要两个参数。正如你在第一个例子中所写的,你正在向它传递一堆无意义的东西。
相反,进行 * 多个 * strcmp检查,并“或”其结果:

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

int main(void)
{
    string username = get_string("What would you like your username to be? ");
    string agreement = get_string("Do you agree to the following terms & conditions? ");
    if (!strcmp(agreement, "yes") || !strcmp(agreement, "y") || !strcmp(agreement, "Yes") || !strcmp(agreement, "Y"))
    {
        printf("You can now continue using the service, %s! \n", username);
    }
    else if (!strcmp(agreement, "no") || !strcmp(agreement, "n") || !strcmp(agreement, "Yes") || !strcmp(agreement, "Y"))
    {
        printf("You need to agree to the terms & conditions to use the service\n");
    }
    else
    {
        printf("You need to select a option\n");
    }
}

字符串

5fjcxozz

5fjcxozz2#

因此,错误是由于这一行:

if (!strcmp(agreement, "yes" || agreement, "y" || agreement, "Yes" || agreement, "Y"))

字符串
strcmp的调用被 * 解析 * 为

strcmp(agreement, ("yes" || agreement), ("y" || agreement), ("Yes || agreement), "Y")


因此出现“参数过多”错误。* 应该写成

if (!strcmp(agreement, "yes") ||  // you're comparing the results
    !strcmp(agreement, "y")   ||  // of four separate calls to
    !strcmp(agreement, "Yes") ||  // strcmp
    !strcmp(agreement, "Y") )
{
  ...
}


因为看起来你只关心agreement的第一个字母,你可以简化一下:

#include <ctype.h>
...
if ( tolower( agreement[0] ) == 'y' ) // convert the first letter of
{                                     // agreement to lower case and
  ...                                 // compare against 'y'
}
else if ( tolower( agreement[0] ) == 'n' )
{
  ...
}
else
{
  ...
}


如果agreement包含"Yes""yes""Y""y""YO, ADRIAN!""yup"等字符串中的任何一个,则此操作将有效。
请注意,CS50 * 严重 * 歪曲了C中的字符串操作。C没有实际的string数据类型;是CS50库所独有的它是类型char *的别名(指向char的指针)。get_string函数在后台分配内存来存储字符串,并返回一个指向该内存的指针,这就是实际存储在agreement中的内容:

+---+          +---+
agreement: |   | -------> |'Y'|  
           +---+          +---+
                          |'e'|
                          +---+
                          |'s'|
                          +---+
                          | 0 |
                          +---+


在C语言中,string 是一个包含零值终止符的字符值序列。字符串"yes"表示为序列{'y', 'e', 's', 0}
字符串 * 存储 * 在字符类型的数组中。你可以在声明中用字符串 * 初始化 * 一个数组:

char greeting[SIZE] = "hello";


或者是

char greeting[SIZE] = {'h', 'e', 'l',  'l', 'o', 0};


但是你不能使用=操作符将数组相互赋值;这行不通:

greeting = "goodbye";


你也可以使用像strcpy这样的库函数:

strcpy( greeting, "goodbye" );


或单独分配每个元素:

greeting[0] = 'g';
greeting[1] = 'o';
greeting[2] = 'o';
...
greeting[6] = 'e';
greeting[7] = 0;


类似地,不能使用==比较数组内容,必须使用strcmpmemcmp等库函数,或者单独比较元素。

相关问题