c - 2 编译器错误预期 ';' 在声明列表的末尾和字段 'x' 声明为函数?

标签 c

我正在做本周的 C 课作业,在编译程序时遇到了两个错误。如果重要的话,我正在使用 repl.it。作业的目的是创建一个程序,将医疗保健记录计算机化。它会提示用户输入一些信息名称、心率、高度、BMI、生日等,然后将它们吐出来供用户查看,但我无法克服这两个该死的错误!

两个错误如下

clang version 7.0.0-3~ubuntu0.18.04.1 (tags/RELEASE_700/final)
exit status 1
main.c:9:14: error: expected ';' at end of declaration list
  void read() {
             ^
             ;
main.c:9:8: error: field 'read' declared as a function
  void read() {
       ^
2 errors generated.

这是我的程序的代码,任何帮助都会很棒!

#include <stdio.h>

struct HealthProfile {

  char firstName[10], lastName[10], gender[2];

  int height, weight, day, month, year, current_year, tHR, maxHR, HR;

  void read() {
    printf("Please enter the patient's last name \n");
    scanf("%s", lastName);
    printf("Please enter the patient's first name \n");
    scanf("%s", firstName);
    printf("Please enter the patient's gender(M/F) \n");
    scanf("%s", gender);
    printf("Please enter the current year \n");
    scanf("%d", &current_year);
    printf("Please enter the patient's birthdate as mm/dd/yyyy \n");
    scanf("%d/%d/%d", &month, &day, &year);
    printf("Please enter the patient's height in inches \n");
    scanf("%d", &height);
    printf("Please enter the patient's weight in pounds \n");
    scanf("%d", &weight);
    printf("Please enter the patient's heart rate \n");
    scanf("%d", &HR);
  }
  int Bmi() {
    return ((703 * weight) / (height * height));
  }
  int age() {
    return (current_year - year);
  }
  void heartRate() {
    /* as no formula and parameters are given for calculating heart rate So defaults has been taken*/

    int maxrate = 220;
    int heartrate = maxrate - age();
    int val = heartrate - HR;

    float res1 = (val * 0.4);
    float res2 = (val * 0.6);

    float targetmin = res1 + HR;
    float targetmax = res2 + HR;

    printf("\nHeart beat low rate: %.1f - %.1f", targetmin, targetmax);

    res1 = (val * 0.6);
    res2 = (val * 0.7);

    targetmin = res1 + HR;
    targetmax = res2 + HR;
    printf("\nHeart beat medium rate: %.1f - %.1f", targetmin, targetmax);

    res1 = (val * 0.7);
    res2 = (val * 0.85);

    targetmin = res1 + HR;
    targetmax = res2 + HR;
    printf("\nHeart beat high rate: %.1f - %.1f", targetmin, targetmax);
  }

  void display() {
    printf("The patient's name %s %s \n", firstName, lastName);
    printf("The patient's gender %s \n", gender);
    printf("The patient's birthdate %d/%d/%d \n", month, day, year);
    printf("The patient's height %d \n", height);
    printf("The patient's weight %d\n", weight);
    printf("The patient's age %d \n", age());
    printf("The patient's BMI %d \n", Bmi());
    heartRate();
  }
};

int main() {
  struct HealthProfile HP;
  HP.read()
  HP.display()

  return 0;
}

最佳答案

这不是 C++; struct 不能包含函数。相反,您必须在 struct 之外声明这些函数,并让它们将结构的句柄作为参数。

int age(const struct HealthProfile *hp)
{
    return (hp->current_year - hp->year);
}

查看 ->. 运算符,它们用于访问和修改结构的成员。

或者,切换到 C++ 编译器并使用 class 而不是 struct

关于c - 2 编译器错误预期 ';' 在声明列表的末尾和字段 'x' 声明为函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55679353/

相关文章:

c - 理解c语言程序的逻辑

C: <sys/stat.h> 函数 S_ISLNK、S_ISDIR 和 S_ISREG 表现异常?

c - 如何使用 Kernel Panic 日志确定内存损坏的原因?

.net - 用 C 编写 .NET 对象?

c++ - Qt 拆分 QString

c - 请求非结构或 union 中的成员?

c - 如何使用相同的 UDP 套接字发送和接收数据包?我在这段代码中缺少什么?

c - 在 C 函数中删除链表节点不会传输到调用函数

c - 将指针存储到数组时哪里出错了?

c - 将标准输出重定向到文件?