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

问题描述 投票:0回答:2

我本周在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
2个回答
2
投票

这不是C ++;一个struct不能包含功能。您必须在struct之外声明这些函数,并让它们将结构作为参数处理。

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

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

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


0
投票

首先,免责声明,对C或C ++不是很有经验,所以这可能不是最好的方法。

我已经将完整的代码上传到pastebin,因为我在这里发布了一些问题。我所做的是将例程移出结构并使例程将结构作为参数。这样结构看起来像这样:

struct HealthProfile { char firstName[10], lastName[10], gender[2]; int height, weight, day, month, year, current_year, tHR, maxHR, HR;};

© www.soinside.com 2019 - 2024. All rights reserved.