gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:03 26 4
gpt4 key购买 nike

我正在做本周的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com