简体   繁体   English

将字符串与 C 中的结构成员进行比较

[英]Comparing a string with a struct member in C

I am attempting to create a small ticket pricing system.我正在尝试创建一个小票定价系统。 The user inputs the people accompanying them to the theme park as a command line argument (senior/adult/child/student) including themselves (what they are according to that list) and I compare the command line arguments with the struct members named tier - this part will be completed later with pointers to check each one, but until then I'm just attempting to get this lesser version of the completed program to work and cannot seem to.用户输入陪同他们到主题公园的人作为命令行参数(高级/成人/儿童/学生),包括他们自己(根据该列表他们是什么),我将命令行 arguments 与名为 tier 的结构成员进行比较 -这部分将在稍后完成,并提供检查每一个的指针,但在那之前,我只是试图让这个较小版本的已完成程序工作,但似乎无法工作。 It won't allow me to compare a struct member (string) with a command line argument.它不允许我将结构成员(字符串)与命令行参数进行比较。 I'm finding rectifying this slightly confusing because the error message我发现纠正这个有点令人困惑,因为错误消息

strcmpare.c:17:29: error: use of undeclared identifier 'senior' if (strcmp(argv[a], senior.tier[a]) == 0)

isn't yielding any clues as to what I should do.没有提供任何关于我应该做什么的线索。 Here's the code so far (I am using the cs50 course and sandbox, thus the cs50.h header file. Apologies in advance, I'm not sure what the equivalent is in C):这是到目前为止的代码(我使用的是 cs50 课程和沙箱,因此是 cs50.h header 文件。提前道歉,我不确定 C 中的等价物是什么):

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

struct ages
{
    char tier[7];
    int price;
};

void structorganizer(struct ages senior, struct ages adult, struct ages child, struct ages student);

int main(int argc, string argv[])
{
    for (int a = 0; a <= (argc-1); a ++)
    {
        if (strcmp(argv[a], senior.tier[a]) == 0)
        {
            printf("no errors");
            return 0;
        }
        printf("scrutinise more");
        return 1;
    }
}

void structorganizer(struct ages senior, struct ages adult, struct ages child, struct ages student)
{
    strcpy(senior.tier, "Senior");
    senior.price = 10;

    strcpy(adult.tier, "Adult");
    adult.price = 30;

    strcpy(child.tier, "Child");
    child.price = 0;

    strcpy (student.tier, "Student");
    student.price = 20;
}

First of all I think you should change the declaration of the struct to this: (I guess it's a good convention to start the structs name with Upper case)首先,我认为您应该将结构的声明更改为:(我想以大写开头的结构名称是一个很好的约定)

typedef struct{
    char tier[7];
    int price;
}Age;

and then in the main, you have to create a new instance, for example:然后在主体中,您必须创建一个新实例,例如:

int main(int argc, string argv[])
{
    Age senior;//HERE
    for (int a = 0; a <= (argc-1); a ++)
    {
        if (strcmp(argv[a], senior.tier[a]) == 0)
        {
            printf("no errors");
            return 0;
        }
        printf("scrutinise more");
        return 1;
    }
}

If you do this also change the struct name in the structorganizer function, like this如果这样做,还要更改结构组织器 function 中的结构名称,如下所示

void structorganizer(Age senior, Age adult, Age child, Age student)

I'm finding rectifying this slightly confusing because the error message isn't yielding any clues as to what I should do.我发现纠正这个有点令人困惑,因为错误消息没有提供任何关于我应该做什么的线索。

On the contrary, it actually tells you EXACTLY what to do.相反,它实际上告诉你该做什么。 That's not always the case with error messages, but here it is.错误消息并非总是如此,但在这里。 The variable senior is not declared, so you need to declare it.变量senior没有声明,所以需要声明。

So where should you do that.那么你应该在哪里做。 You seem to want to use structorganizer to initialize things.您似乎想使用structorganizer来初始化事物。 Usually, global variables are a thing you want to avoid and there are certainly better ways of solving this, but just to quickly get things working.通常,全局变量是您想要避免的事情,当然有更好的方法来解决这个问题,但只是为了快速让事情正常工作。 Remove all arguments from the function and declare global variables before, and call structorganizer first thing in main like this:从 function 中删除所有 arguments 并在之前声明全局变量,然后在main中首先调用structorganizer ,如下所示:

struct ages
{
    char tier[8]; // Changed to 8 because you need one more than the string length
    int price;
};

// Evil global variables, just to make code work with as little
// changes as possible
struct ages senior, adult, ages, child, student;

void structorganizer(); // Init the evil globals

int main(int argc, string argv[])
{
    structorganizer();

    // Continue as before

That should be enough to just get the code working, but you clearly need to spend more time with the basics here.这应该足以让代码正常工作,但您显然需要在这里花更多时间了解基础知识。 The code you're writing right now is too advanced for you.你现在写的代码对你来说太高级了。 No shame in trying though:)尝试并不羞耻:)

There are absolutely cases where it's good to use an initializer function, but I cannot really say that this is one of them.在某些情况下,使用初始化程序 function 是很好的,但我不能说这是其中之一。 I would declare them in main like this:我会像这样在main中声明它们:

int main(int argc, string argv[])
{
    struct ages senior   = { .tier = "Senior",   .price=10 };
    struct ages adult    = { .tier = "Adult",    .price=30 };
    struct ages child    = { .tier = "Child",    .price=0  };
    struct ages student  = { .tier = "Student",  .price=20 };
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM