获取用户输入并存储到结构中的元素中

Getting User Input and storing into elements in Structure

我目前正在编写一个简单的代码,重复读取用户输入并将输入存储到一个结构中,随后将它们打印出来。我在阅读 "accountNum" 和 "balance" 时遇到问题。编译器给出的警告是由于预期的不同参数类型。 (*int/*double 对比 int/double)。 我尝试使用 gets() 但无济于事。我希望对此有所了解。

此外,我是否在打印功能期间正确访问了元素?提前致谢!

#include <stdio.h>
#include <string.h>
struct account {
    struct
    {
        char lastName[10];
        char firstName[10];
    } names;
    int accountNum;
    double balance;
    };

    void nextCustomer(struct account *acct);
    void printCustomer(struct account acct);
    int main()
    {
    struct account record;
    int flag = 0;
    do {
        nextCustomer(&record);
        if ((strcmp(record.names.firstName, "End") == 0) &&
            (strcmp(record.names.lastName, "Customer") == 0))
            flag = 1;
        if (flag != 1)
            printCustomer(record);
        } while (flag != 1);
        }

        void nextCustomer(struct account *acct) {
        printf("Enter names: (firstName lastName): " );
        scanf("%s%s", acct->names.firstName, acct->names.lastName);
        printf("Enter account number: ");
        scanf("%d", acct->accountNum);
        printf("Enter balance : ");
        scanf("%lf", acct->balance);
        }

        void printCustomer(struct account acct) {
        printf("%s%s %d %lf", acct.names.firstName, acct.names.lastName ,acct.accountNum,acct.balance);
       }
  1. 得到intdouble

    scanf("%d", &acct->accountNum);
    scanf("%lf", &acct->balance);
    

    &acct->accountNum 是指向 int&acct->balance 是指向 double

  2. 的指针
  3. 第二个scanf你忘记了'%'

  4. 不建议出于任何目的使用 gets(),它现在被视为已弃用的功能。阅读更多 here