简体   繁体   中英

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. 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'.

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 .

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

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.

The fourth problem is that the structures id member can only contain four-character strings, since the null-terminator needs one element. 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.

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.

The seventh problem is that you treat the structures id member as both a string and as a single integer value. 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.

The "not a structure or union" error is because the type of record in record.id is not a structure or union. record is an array of 5 pointers to struct , so the correct syntax would be 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 .

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.

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. 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). 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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