简体   繁体   English

在 C 中检查登录信息的结构

[英]Structs to check login information in C

I have to create a program that only allows 5 people to login with predetermined user ID's and passwords.我必须创建一个程序,它只允许 5 人使用预定的用户 ID 和密码登录。 I think defining them within a function then saving them in a struct would be the best way to do this.我认为在函数中定义它们然后将它们保存在结构中将是最好的方法。 So far I have:到目前为止,我有:

    struct nurse
{
    char id[5];
    char password[5][8];
}*record[5];

int main(void)
{

    record.id=1;
    strcopy(record.id, "username");
    strcopy(record.password, "password");

    printf("Please Enter your username: ");
    scanf("%s", id);
    if (id != username)
    {
        printf("Error. Record not found.");
    }

I'm unfortunately coming up with error messages saying "[Error] request for member 'id' in something not a structure or union" and the same error for 'password'.不幸的是,我想出了错误消息,说“[错误] 在非结构或联合的东西中请求成员‘id’”和“密码”同样的错误。

How can I solve this?我该如何解决这个问题?

You have multiple errors in the code you show.您显示的代码中有多个错误。

The first is that record is an array of five pointers to struct nurse .第一个是record是一个包含五个指向struct nurse指针的数组。

It should be an array of structure object:它应该是一个结构对象数组:

struct nurse { ... } record[5];  // No *, no pointers

The second problem is that you don't have a variable named id so第二个问题是你没有一个名为id的变量所以

scanf("%s", id);

will not work.不管用。

The third problem is that you use the unknown function strcopy , when I believe you mean to use the standard strcpy function.第三个问题是您使用了未知函数strcopy ,当我相信您的意思是使用标准strcpy函数时。

The fourth problem is that the structures id member can only contain four-character strings, since the null-terminator needs one element.第四个问题是结构id成员只能包含四个字符的字符串,因为空终止符需要一个元素。 And you try to copy (in the example shown) a much longer string into that array.并且您尝试将(在所示示例中)一个更长的字符串复制到该数组中。

The fifth problem is that you can't use == or != to compare strings.第五个问题是你不能使用==!=来比较字符串。 You should use the strcmp function to compare strings.您应该使用strcmp函数来比较字符串。

The sixth problem is that the password member of the structure is an array of arrays , and you try to copy a string to password as it was a single string.第六个问题是结构体的password成员是一个数组数组,您尝试将一个字符串复制到password因为它是单个字符串。

The seventh problem is that you treat the structures id member as both a string and as a single integer value.第七个问题是您将结构id成员视为字符串和单个整数值。 It can't be both.不能两者兼而有之。

There might be more problems, but these should be good enough to start with.可能还有更多问题,但这些应该足以开始。

And all in all it seems you're missing some basic C knowledge, and could use a few books to read or classes to attend.总而言之,您似乎缺少一些基本的 C 知识,并且可以使用几本书来阅读或上课。

The "not a structure or union" error is because the type of record in record.id is not a structure or union. “不是结构或联合”错误是因为record.idrecord类型不是结构或联合。 record is an array of 5 pointers to struct , so the correct syntax would be record[0]->id . record是一个包含 5 个指向struct指针的数组,因此正确的语法是record[0]->id But you probably didn't want an array of pointers, so if you change the *record[5] to record[5] , you could access the id of the first record as record[0].id or record->id .但是您可能不想要指针数组,因此如果您将*record[5]更改为record[5] ,您可以访问第一条记录的id作为record[0].idrecord->id

There are numerous other problems as well, on practically every line of the program, so I can only suggest spending more time learning the basics of C.程序的几乎每一行都存在许多其他问题,所以我只能建议花更多时间学习 C 的基础知识。

In general, C is a fairly concise and small language, so consider the meaning of each symbol carefully (eg, an extra * makes all the difference) before you type it in. And instead of scattering literal numbers (like 5 and 8 ) throughout your code, #define constants and use those instead.一般来说,C 是一种相当简洁和小型的语言,所以在输入之前仔细考虑每个符号的含义(例如,一个额外的*使一切不同)。而不是散布文字数字(如58 )您的代码,# #define常量并使用它们。 For example, it is now unclear whether id[5] has the 5 because you want there to be five ids in total, or because you want each individual id to be five characters long (in which case you are missing room for the NUL, or zero, terminator).例如,现在不清楚id[5]是否有5是因为您希望总共有五个 id,还是因为您希望每个单独的 id 有五个字符长(在这种情况下,您缺少 NUL 的空间,或零,终止符)。 For example:例如:

#define NUM_RECORDS 5
#define ID_LENGTH 5
#define PASSWORD_LENGTH 8

struct nurse {
    char id[ID_LENGTH + 1];
    char password[PASSWORD_LENGTH + 1];
} record[NUM_RECORDS];

(You may later opt to use a more succinct style than this, but when getting started I believe it is better to be explicit.) (你以后可能会选择使用比这更简洁的风格,但在开始时,我认为最好是明确的。)

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

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